@zeewain/3d-avatar-sdk 1.2.5 → 2.1.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.esm.js CHANGED
@@ -107,6 +107,8 @@ var ResourceErrorCode;
107
107
  ResourceErrorCode[ResourceErrorCode["NOT_FOUND"] = 3003] = "NOT_FOUND";
108
108
  /** 资源格式不支持 */
109
109
  ResourceErrorCode[ResourceErrorCode["UNSUPPORTED_FORMAT"] = 3004] = "UNSUPPORTED_FORMAT";
110
+ /** 资源版本不兼容 */
111
+ ResourceErrorCode[ResourceErrorCode["VERSION_INCOMPATIBLE"] = 3005] = "VERSION_INCOMPATIBLE";
110
112
  })(ResourceErrorCode || (ResourceErrorCode = {}));
111
113
  /**
112
114
  * 系统错误码 (4xxx)
@@ -213,6 +215,10 @@ const ERROR_CODE_MAP = {
213
215
  category: ErrorCategory.RESOURCE,
214
216
  message: '资源格式不支持'
215
217
  },
218
+ [ResourceErrorCode.VERSION_INCOMPATIBLE]: {
219
+ category: ErrorCategory.RESOURCE,
220
+ message: '资源版本不兼容'
221
+ },
216
222
  // 系统错误
217
223
  [SystemErrorCode.OUT_OF_MEMORY]: {
218
224
  category: ErrorCategory.SYSTEM,
@@ -401,6 +407,44 @@ class SDKError extends Error {
401
407
  }
402
408
  }
403
409
 
410
+ /**
411
+ * Unity服务日志级别枚举
412
+ * @enum {string}
413
+ * @description 定义日志记录的级别
414
+ */
415
+ var LogLevel;
416
+ (function (LogLevel) {
417
+ LogLevel["DEBUG"] = "debug";
418
+ LogLevel["INFO"] = "info";
419
+ LogLevel["WARN"] = "warn";
420
+ LogLevel["ERROR"] = "error";
421
+ })(LogLevel || (LogLevel = {}));
422
+ /**
423
+ * 简单日志记录器实现
424
+ * @class SimpleLogger
425
+ * @implements {ISimpleLogger}
426
+ * @description 提供基础的日志记录功能
427
+ */
428
+ class SimpleLogger {
429
+ constructor(enableDebug = false) {
430
+ this.enableDebug = enableDebug;
431
+ }
432
+ debug(message, data) {
433
+ if (this.enableDebug) {
434
+ console.debug(`[SDK DEBUG] ${message}`, data);
435
+ }
436
+ }
437
+ info(message, data) {
438
+ console.info(`[SDK INFO] ${message}`, data);
439
+ }
440
+ warn(message, data) {
441
+ console.warn(`[SDK WARN] ${message}`, data);
442
+ }
443
+ error(message, error, data) {
444
+ console.error(`[SDK ERROR] ${message}`, error, error instanceof SDKError ? error.code : null, data);
445
+ }
446
+ }
447
+
404
448
  /**
405
449
  * @fileoverview Unity服务基础类型定义
406
450
  * @description 定义Unity服务的通用接口和类型,为所有Unity服务提供统一的基础类型
@@ -421,18 +465,6 @@ var UnityOperationStatus;
421
465
  /** 操作取消 */
422
466
  UnityOperationStatus[UnityOperationStatus["CANCELLED"] = 3] = "CANCELLED";
423
467
  })(UnityOperationStatus || (UnityOperationStatus = {}));
424
- /**
425
- * Unity服务日志级别枚举
426
- * @enum {string}
427
- * @description 定义日志记录的级别
428
- */
429
- var LogLevel;
430
- (function (LogLevel) {
431
- LogLevel["DEBUG"] = "debug";
432
- LogLevel["INFO"] = "info";
433
- LogLevel["WARN"] = "warn";
434
- LogLevel["ERROR"] = "error";
435
- })(LogLevel || (LogLevel = {}));
436
468
 
437
469
  /**
438
470
  * @fileoverview Unity服务抽象基类
@@ -717,31 +749,6 @@ class UnityBaseService {
717
749
  });
718
750
  }
719
751
  }
720
- /**
721
- * 简单日志记录器实现
722
- * @class SimpleLogger
723
- * @implements {IUnityLogger}
724
- * @description 提供基础的日志记录功能
725
- */
726
- class SimpleLogger {
727
- constructor(enableDebug = false) {
728
- this.enableDebug = enableDebug;
729
- }
730
- debug(message, data) {
731
- if (this.enableDebug) {
732
- console.debug(`[Unity Debug] ${message}`, data);
733
- }
734
- }
735
- info(message, data) {
736
- console.info(`[Unity Info] ${message}`, data);
737
- }
738
- warn(message, data) {
739
- console.warn(`[Unity Warning] ${message}`, data);
740
- }
741
- error(message, error, data) {
742
- console.error(`[Unity Error] ${message}`, error, error instanceof SDKError ? error.code : null, data);
743
- }
744
- }
745
752
 
746
753
  /**
747
754
  * @fileoverview Avatar API接口定义
@@ -836,6 +843,9 @@ class AvatarService extends UnityBaseService {
836
843
  }
837
844
  });
838
845
  }
846
+ handleCallback(operation, code, message, data) {
847
+ super.handleCallback(operation, code, message, data);
848
+ }
839
849
  /**
840
850
  * 播放数字人动作
841
851
  * @param clipCode - 动作编码
@@ -1073,6 +1083,16 @@ class ConfigManager {
1073
1083
  setConfig(config) {
1074
1084
  this.config = Object.assign(Object.assign({}, config), { env: config.env || 'prod', containerId: config.containerId || 'unity-container' });
1075
1085
  }
1086
+ /**
1087
+ * 更新SDK配置
1088
+ * @param config - SDK配置对象
1089
+ * @description 更新全局SDK配置
1090
+ */
1091
+ updateConfig(config) {
1092
+ if (this.config) {
1093
+ this.config = Object.assign(Object.assign({}, this.config), config);
1094
+ }
1095
+ }
1076
1096
  /**
1077
1097
  * 获取SDK配置
1078
1098
  * @returns IAvatarSDKConfig | null SDK配置对象或null
@@ -1081,24 +1101,6 @@ class ConfigManager {
1081
1101
  getConfig() {
1082
1102
  return this.config;
1083
1103
  }
1084
- /**
1085
- * 获取认证token
1086
- * @returns string 认证token
1087
- * @description 获取当前配置的认证token
1088
- */
1089
- getToken() {
1090
- var _a;
1091
- return ((_a = this.config) === null || _a === void 0 ? void 0 : _a.token) || '';
1092
- }
1093
- /**
1094
- * 获取环境类型
1095
- * @returns string 环境类型
1096
- * @description 获取当前配置的环境类型
1097
- */
1098
- getEnv() {
1099
- var _a;
1100
- return ((_a = this.config) === null || _a === void 0 ? void 0 : _a.env) || 'prod';
1101
- }
1102
1104
  /**
1103
1105
  * 获取API基础URL
1104
1106
  * @param withApiModule 是否包含模块路径
@@ -1113,15 +1115,6 @@ class ConfigManager {
1113
1115
  }
1114
1116
  return ((_d = getEnvConfig(((_c = this.config) === null || _c === void 0 ? void 0 : _c.env) || 'prod', withApiModule)) === null || _d === void 0 ? void 0 : _d.apiBaseUrl) || '';
1115
1117
  }
1116
- /**
1117
- * 获取自定义API URL
1118
- * @returns string | undefined 自定义API URL
1119
- * @description 获取自定义API URL配置
1120
- */
1121
- getCustomApiUrl() {
1122
- var _a;
1123
- return (_a = this.config) === null || _a === void 0 ? void 0 : _a.apiUrl;
1124
- }
1125
1118
  /**
1126
1119
  * 重置配置
1127
1120
  * @description 清空当前配置
@@ -1891,6 +1884,7 @@ class BroadcastService extends UnityBaseService {
1891
1884
  */
1892
1885
  startTaskRequest(task) {
1893
1886
  return __awaiter(this, void 0, void 0, function* () {
1887
+ var _a;
1894
1888
  task.status = BroadcastTaskStatus.REQUESTING;
1895
1889
  this.logger.debug('Starting task request', { taskId: task.id });
1896
1890
  try {
@@ -1899,7 +1893,8 @@ class BroadcastService extends UnityBaseService {
1899
1893
  humanCode: task.params.humanCode,
1900
1894
  speed: task.params.speed,
1901
1895
  volume: task.params.volume >= 0 ? task.params.volume * 100 : undefined,
1902
- isSubtitle: task.params.isSubtitle
1896
+ isSubtitle: task.params.isSubtitle,
1897
+ audioDrivenVersion: ConfigManager.getInstance().getConfig().audioDrivenVersion
1903
1898
  };
1904
1899
  // 根据播报类型设置特定参数
1905
1900
  if (task.params.type === BroadcastType.TEXT) {
@@ -1915,7 +1910,7 @@ class BroadcastService extends UnityBaseService {
1915
1910
  method: 'POST',
1916
1911
  headers: {
1917
1912
  'Content-Type': 'application/json',
1918
- 'x_auth_token': ConfigManager.getInstance().getToken()
1913
+ 'x_auth_token': ((_a = ConfigManager.getInstance().getConfig()) === null || _a === void 0 ? void 0 : _a.token) || ''
1919
1914
  },
1920
1915
  body: JSON.stringify(requestBody),
1921
1916
  signal: task.controller.signal,
@@ -2077,7 +2072,6 @@ class BroadcastService extends UnityBaseService {
2077
2072
  * @private
2078
2073
  */
2079
2074
  getBroadcastApiPath(type) {
2080
- ConfigManager.getInstance().getEnv();
2081
2075
  switch (type) {
2082
2076
  case BroadcastType.TEXT:
2083
2077
  return '/aiep-openapi/avatar-interaction/v1/broadcast/text';
@@ -2171,13 +2165,11 @@ class ZEEAvatarLoader {
2171
2165
  * @param config - Unity配置对象
2172
2166
  * @description 初始化数字人加载器,创建Unity加载器实例
2173
2167
  */
2174
- constructor(config) {
2168
+ constructor() {
2175
2169
  /** Avatar API实例 */
2176
2170
  this.apiService = null;
2177
2171
  /** Unity实例 */
2178
2172
  this.unityInstance = null;
2179
- // 同时设置到配置管理器中
2180
- ConfigManager.getInstance().setConfig(config);
2181
2173
  this.loader = new UnityLoader();
2182
2174
  }
2183
2175
  /**
@@ -2280,17 +2272,61 @@ class ZEEAvatarLoader {
2280
2272
  * @protected
2281
2273
  */
2282
2274
  initGlobalConfig() {
2283
- const params = {
2284
- token: ConfigManager.getInstance().getToken(),
2275
+ const config = ConfigManager.getInstance().getConfig();
2276
+ const globalParams = {
2277
+ token: config === null || config === void 0 ? void 0 : config.token,
2285
2278
  apiBaseUrl: ConfigManager.getInstance().getApiBaseUrl(false),
2286
- idleMotionList: ConfigManager.getInstance().getConfig().idleMotionList,
2287
- assetsUrl: ConfigManager.getInstance().getConfig().assetsUrl
2279
+ idleMotionList: config === null || config === void 0 ? void 0 : config.idleMotionList
2280
+ // 純AB包方案在SDK 2.1.0 中已弃用
2281
+ // assetsUrl: config?.assetsUrl
2282
+ };
2283
+ const assetModuleParams = {
2284
+ isZip: true,
2285
+ assetBundlePath: config === null || config === void 0 ? void 0 : config.assetsUrl
2288
2286
  };
2289
- console.warn('[ Send Unity message ]: AvatarSDK.InitializeConfig', params);
2290
- this.unityInstance.SendMessage('AvatarSDK', 'InitializeConfig', JSON.stringify(params));
2287
+ this.unityInstance.SendMessage('AvatarSDK', 'InitializeConfig', JSON.stringify(globalParams));
2288
+ this.unityInstance.SendMessage('AvatarSDK', 'InitAssetBundleModule', JSON.stringify(assetModuleParams));
2289
+ console.warn('[ Send Unity message ]: AvatarSDK.InitializeConfig', globalParams);
2290
+ console.warn('[ Send Unity message ]: AvatarSDK.InitAssetBundleModule', assetModuleParams);
2291
2291
  }
2292
2292
  }
2293
2293
 
2294
+ // 深拷贝
2295
+ /**
2296
+ * 比较两个版本号的前两位(major.minor)是否一致
2297
+ * @param version1 版本号1,格式:x.x.x
2298
+ * @param version2 版本号2,格式:x.x.x
2299
+ * @returns boolean 如果前两位一致返回 true,否则返回 false
2300
+ * @description 用于检查 SDK 版本与资源版本是否兼容
2301
+ * @example
2302
+ * compareVersionCompatibility('2.1.0', '2.1.5') // true
2303
+ * compareVersionCompatibility('2.1.0', '2.2.0') // false
2304
+ * compareVersionCompatibility('2.1.0', '3.0.0') // false
2305
+ */
2306
+ function compareVersionCompatibility(version1, version2) {
2307
+ // 提取版本号的前两位(major.minor)
2308
+ const getMajorMinor = (version) => {
2309
+ const parts = version.split('.');
2310
+ if (parts.length < 2) {
2311
+ return version;
2312
+ }
2313
+ return `${parts[0]}.${parts[1]}`;
2314
+ };
2315
+ const v1MajorMinor = getMajorMinor(version1);
2316
+ const v2MajorMinor = getMajorMinor(version2);
2317
+ return v1MajorMinor === v2MajorMinor;
2318
+ }
2319
+
2320
+ /**
2321
+ * @fileoverview SDK 版本号常量
2322
+ * @description 此文件由构建脚本自动生成,请勿手动修改
2323
+ */
2324
+ /**
2325
+ * SDK 版本号
2326
+ * @const {string} SDK_VERSION
2327
+ */
2328
+ const SDK_VERSION = '2.1.1';
2329
+
2294
2330
  /**
2295
2331
  * @fileoverview 统一的3D数字人SDK入口类
2296
2332
  * @description 提供统一的SDK接口,内部通过组合模式调用各个服务模块
@@ -2323,10 +2359,12 @@ class ZEEAvatarSDK {
2323
2359
  this.unityInstance = null;
2324
2360
  /** SDK初始化状态 */
2325
2361
  this.isInitialized = false;
2326
- this.config = config;
2327
2362
  this.instanceId = generateUniqueId();
2328
2363
  // 设置全局配置
2329
2364
  ConfigManager.getInstance().setConfig(config);
2365
+ // 设置日志
2366
+ this.logger = new SimpleLogger(ConfigManager.getInstance().getConfig().enableDebugLog);
2367
+ this.logger.info('SDK版本', SDK_VERSION);
2330
2368
  }
2331
2369
  /**
2332
2370
  * 初始化数字人
@@ -2337,12 +2375,14 @@ class ZEEAvatarSDK {
2337
2375
  */
2338
2376
  initializeAvatar(avatarCode_1) {
2339
2377
  return __awaiter(this, arguments, void 0, function* (avatarCode, cameraType = AvatarCameraType.WHOLE) {
2378
+ var _a;
2340
2379
  if (this.isInitialized) {
2341
2380
  throw new SDKError(OperationErrorCode.OPERATION_FAILED, 'SDK已经初始化,请勿重复初始化');
2342
2381
  }
2343
2382
  try {
2383
+ const config = ConfigManager.getInstance().getConfig();
2344
2384
  // 1. 创建Unity加载器
2345
- this.loader = new ZEEAvatarLoader(this.config);
2385
+ this.loader = new ZEEAvatarLoader();
2346
2386
  // 2. 初始化Unity实例
2347
2387
  yield this.loader.init();
2348
2388
  // 3. 获取Unity实例
@@ -2351,18 +2391,31 @@ class ZEEAvatarSDK {
2351
2391
  this.avatarService = new AvatarService({
2352
2392
  unityInstance: this.unityInstance,
2353
2393
  instanceId: this.instanceId,
2354
- enableDebugLog: this.config.enableDebugLog
2394
+ enableDebugLog: config.enableDebugLog
2355
2395
  });
2356
2396
  // 5. 创建带有唯一标识符的播报服务
2357
2397
  this.broadcastService = new BroadcastService({
2358
2398
  unityInstance: this.unityInstance,
2359
2399
  instanceId: this.instanceId,
2360
- callbacks: this.config.broadcastCallbacks,
2361
- enableDebugLog: this.config.enableDebugLog
2400
+ callbacks: config.broadcastCallbacks,
2401
+ enableDebugLog: config.enableDebugLog
2362
2402
  });
2363
2403
  // 6. 初始化数字人
2364
2404
  const result = yield this.avatarService.initializeAvatar(avatarCode, cameraType);
2365
2405
  if (result.success) {
2406
+ const audioDrivenVersion = (_a = result.data) === null || _a === void 0 ? void 0 : _a.audioDrivenVersion;
2407
+ config.audioDrivenVersion = audioDrivenVersion;
2408
+ this.logger.info('AudioDrivenVersion', audioDrivenVersion);
2409
+ // 7. 检查资源版本兼容性
2410
+ if (audioDrivenVersion) {
2411
+ const isCompatible = compareVersionCompatibility(SDK_VERSION, audioDrivenVersion);
2412
+ if (!isCompatible) {
2413
+ // 销毁SDK实例
2414
+ this.destroy();
2415
+ throw new SDKError(ResourceErrorCode.VERSION_INCOMPATIBLE, `资源版本不兼容: SDK版本为 ${SDK_VERSION},WebGL资源版本为 ${audioDrivenVersion},前两位版本号必须一致`);
2416
+ }
2417
+ this.logger.info('版本兼容性检查通过', `SDK: ${SDK_VERSION}, 资源: ${audioDrivenVersion}`);
2418
+ }
2366
2419
  this.isInitialized = true;
2367
2420
  }
2368
2421
  return result;
@@ -2378,8 +2431,7 @@ class ZEEAvatarSDK {
2378
2431
  * @description 更新全局Token配置
2379
2432
  */
2380
2433
  updateToken(token) {
2381
- this.config.token = token;
2382
- ConfigManager.getInstance().setConfig(this.config);
2434
+ ConfigManager.getInstance().updateConfig({ token });
2383
2435
  // 如果loader已经初始化,也更新loader的token
2384
2436
  if (this.loader) {
2385
2437
  this.loader.updateToken(token);
@@ -2563,7 +2615,8 @@ class ZEEAvatarSDK {
2563
2615
  * @returns IAvatarSDKConfig 当前配置
2564
2616
  */
2565
2617
  getConfig() {
2566
- return Object.assign({}, this.config);
2618
+ const config = ConfigManager.getInstance().getConfig();
2619
+ return config ? Object.assign({}, config) : null;
2567
2620
  }
2568
2621
  // ===========================================
2569
2622
  // 私有方法
@@ -2579,4 +2632,4 @@ class ZEEAvatarSDK {
2579
2632
  }
2580
2633
  }
2581
2634
 
2582
- export { AvatarCameraType, AvatarOperationType, AvatarService, BroadcastOperationType, BroadcastService, BroadcastType, ConfigErrorCode, ERROR_CODE_MAP, ErrorCategory, LogLevel, NetworkErrorCode, OperationErrorCode, ResourceErrorCode, SDKError, SystemErrorCode, UnityOperationStatus, ZEEAvatarLoader, ZEEAvatarSDK, getErrorInfo };
2635
+ export { AvatarCameraType, AvatarOperationType, AvatarService, BroadcastOperationType, BroadcastService, BroadcastType, ConfigErrorCode, ERROR_CODE_MAP, ErrorCategory, NetworkErrorCode, OperationErrorCode, ResourceErrorCode, SDKError, SDK_VERSION, SystemErrorCode, UnityOperationStatus, ZEEAvatarLoader, ZEEAvatarSDK, getErrorInfo };