customer-chat-sdk 1.0.48 → 1.0.50

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.
@@ -14630,13 +14630,13 @@ class ScreenshotManager {
14630
14630
  const configStr = typeof data === 'string' ? data : JSON.stringify(data);
14631
14631
  const config = JSON.parse(configStr);
14632
14632
  // 检查是否包含二进制配置所需的字段
14633
- if (typeof config.sign === 'number' &&
14633
+ if (typeof config.sign === 'string' &&
14634
14634
  typeof config.type === 'number' &&
14635
14635
  typeof config.topic === 'string' &&
14636
14636
  typeof config.routingKey === 'string' &&
14637
14637
  typeof config.ttl === 'number') {
14638
14638
  return {
14639
- sign: config.sign,
14639
+ sign: String(config.sign),
14640
14640
  type: config.type,
14641
14641
  topic: config.topic,
14642
14642
  routingKey: config.routingKey,
@@ -14683,7 +14683,9 @@ class ScreenshotManager {
14683
14683
  try {
14684
14684
  await this.takeScreenshot();
14685
14685
  // 如果配置了二进制模式,发送二进制数据
14686
- if (this.currentBinaryConfig) {
14686
+ // 注意:如果启用了压缩,会在 Worker 压缩完成后自动发送(在 onmessage 中处理)
14687
+ // 如果没有启用压缩,立即发送
14688
+ if (this.currentBinaryConfig && !this.options.compress) {
14687
14689
  const latestScreenshot = this.getLatestScreenshot();
14688
14690
  if (latestScreenshot) {
14689
14691
  try {
@@ -14724,6 +14726,12 @@ class ScreenshotManager {
14724
14726
  }
14725
14727
  }
14726
14728
  }
14729
+ else if (this.currentBinaryConfig && this.options.compress) {
14730
+ // 启用了压缩,等待 Worker 压缩完成后在 onmessage 中发送
14731
+ if (!this.options.silentMode) {
14732
+ console.log('📸 [轮询] 等待 Worker 压缩完成后发送到 iframe...');
14733
+ }
14734
+ }
14727
14735
  }
14728
14736
  catch (error) {
14729
14737
  if (!this.options.silentMode) {
@@ -14911,9 +14919,32 @@ class ScreenshotManager {
14911
14919
  });
14912
14920
  }
14913
14921
  else {
14922
+ // Worker 不可用,如果配置了二进制模式,直接发送原始截图
14914
14923
  if (!this.options.silentMode) {
14915
14924
  console.warn('📸 ⚠️ Worker 不可用,跳过压缩(使用原始截图)');
14916
14925
  }
14926
+ // Worker 不可用时,如果配置了二进制模式,立即发送原始截图
14927
+ if (this.currentBinaryConfig && this.sendToIframeCallback) {
14928
+ const latestScreenshot = this.getLatestScreenshot();
14929
+ if (latestScreenshot) {
14930
+ try {
14931
+ const imageBuffer = this.dataUrlToArrayBuffer(latestScreenshot);
14932
+ const configBuffer = this.buildBinaryConfig(this.currentBinaryConfig);
14933
+ const combinedBuffer = this.combineBinaryData(configBuffer, imageBuffer);
14934
+ const message = {
14935
+ type: 'screenshotBinary',
14936
+ data: combinedBuffer
14937
+ };
14938
+ this.sendToIframeCallback(message);
14939
+ if (!this.options.silentMode) {
14940
+ console.log('📸 [Worker 不可用] ✅ 原始截图已发送到 iframe');
14941
+ }
14942
+ }
14943
+ catch (error) {
14944
+ console.error('📸 [Worker 不可用] ❌ 发送原始截图失败:', error);
14945
+ }
14946
+ }
14947
+ }
14917
14948
  }
14918
14949
  }
14919
14950
  this.error = null;
@@ -16930,21 +16961,30 @@ class ScreenshotManager {
16930
16961
  const { type, data } = e.data;
16931
16962
  if (type === 'SCREENSHOT_RESULT' && data?.compressed) {
16932
16963
  const compressed = data.compressed;
16933
- // 更新截图历史记录
16934
- if (this.screenshotHistory.length > 0) {
16964
+ // 更新截图历史记录(压缩成功或失败都会更新)
16965
+ if (this.screenshotHistory.length > 0 && compressed.dataUrl) {
16935
16966
  this.screenshotHistory[this.screenshotHistory.length - 1] = compressed.dataUrl;
16936
16967
  // 打印压缩统计信息
16937
- if (!this.options.silentMode && compressed.originalSize && compressed.compressedSize) {
16938
- const originalKB = (compressed.originalSize * 0.75 / 1024).toFixed(2);
16939
- const compressedKB = (compressed.compressedSize * 0.75 / 1024).toFixed(2);
16940
- const ratio = compressed.compressionRatio || '0';
16941
- console.log('📸 [Worker 压缩] ✅ 压缩完成');
16942
- console.log(` 原始大小: ${originalKB} KB`);
16943
- console.log(` 压缩后: ${compressedKB} KB`);
16944
- console.log(` 压缩率: ${ratio}%`);
16968
+ if (!this.options.silentMode) {
16945
16969
  if (compressed.error) {
16946
- console.warn(` ⚠️ 压缩警告: ${compressed.error}`);
16970
+ // 压缩失败,使用原始数据
16971
+ console.warn('📸 [Worker 压缩] ⚠️ 压缩失败,使用原始截图');
16972
+ console.warn(` ⚠️ 错误: ${compressed.error}`);
16947
16973
  }
16974
+ else if (compressed.originalSize && compressed.compressedSize) {
16975
+ // 压缩成功,显示统计信息
16976
+ const originalKB = (compressed.originalSize * 0.75 / 1024).toFixed(2);
16977
+ const compressedKB = (compressed.compressedSize * 0.75 / 1024).toFixed(2);
16978
+ const ratio = compressed.compressionRatio || '0';
16979
+ console.log('📸 [Worker 压缩] ✅ 压缩完成');
16980
+ console.log(` 原始大小: ${originalKB} KB`);
16981
+ console.log(` 压缩后: ${compressedKB} KB`);
16982
+ console.log(` 压缩率: ${ratio}%`);
16983
+ }
16984
+ }
16985
+ // 压缩完成后(无论成功或失败),如果配置了二进制模式,发送数据到 iframe
16986
+ if (this.currentBinaryConfig && compressed.dataUrl) {
16987
+ this.sendCompressedScreenshotToIframe(compressed.dataUrl);
16948
16988
  }
16949
16989
  }
16950
16990
  }
@@ -16954,6 +16994,28 @@ class ScreenshotManager {
16954
16994
  if (!this.options.silentMode) {
16955
16995
  console.warn('📸 Worker 压缩失败,使用原始截图');
16956
16996
  }
16997
+ // Worker 发生错误时,如果配置了二进制模式,发送原始截图
16998
+ if (this.currentBinaryConfig && this.sendToIframeCallback) {
16999
+ const latestScreenshot = this.getLatestScreenshot();
17000
+ if (latestScreenshot) {
17001
+ try {
17002
+ const imageBuffer = this.dataUrlToArrayBuffer(latestScreenshot);
17003
+ const configBuffer = this.buildBinaryConfig(this.currentBinaryConfig);
17004
+ const combinedBuffer = this.combineBinaryData(configBuffer, imageBuffer);
17005
+ const message = {
17006
+ type: 'screenshotBinary',
17007
+ data: combinedBuffer
17008
+ };
17009
+ this.sendToIframeCallback(message);
17010
+ if (!this.options.silentMode) {
17011
+ console.log('📸 [Worker 错误] ✅ 原始截图已发送到 iframe');
17012
+ }
17013
+ }
17014
+ catch (error) {
17015
+ console.error('📸 [Worker 错误] ❌ 发送原始截图失败:', error);
17016
+ }
17017
+ }
17018
+ }
16957
17019
  };
16958
17020
  // 注意:不要立即 revokeObjectURL,因为 Worker 需要这个 URL 保持有效
16959
17021
  // 在 destroy() 方法中清理 Worker 时再 revoke
@@ -17034,7 +17096,11 @@ class ScreenshotManager {
17034
17096
  const encoder = new TextEncoder();
17035
17097
  let offset = 0;
17036
17098
  // sign: 8字节 (BigInt64)
17037
- view.setBigInt64(offset, BigInt(config.sign));
17099
+ // 将字符串转换为 BigInt(支持数字字符串,如 "1234567890")
17100
+ const signValue = typeof config.sign === 'string'
17101
+ ? BigInt(config.sign)
17102
+ : BigInt(String(config.sign));
17103
+ view.setBigInt64(offset, signValue);
17038
17104
  offset += 8;
17039
17105
  // type: 1字节 (Uint8)
17040
17106
  view.setUint8(offset, config.type);
@@ -17082,53 +17148,63 @@ class ScreenshotManager {
17082
17148
  if (success) {
17083
17149
  // 截图完成后,等待一小段时间确保数据已保存
17084
17150
  await new Promise(resolve => setTimeout(resolve, 100));
17085
- // 获取最新截图并转换为二进制
17086
- const latestScreenshot = this.getLatestScreenshot();
17087
- if (latestScreenshot) {
17088
- try {
17089
- // 计算 base64 大小
17090
- const base64Data = latestScreenshot.split(',')[1] || '';
17091
- const base64Size = base64Data.length;
17092
- // 将截图转换为 ArrayBuffer
17093
- const imageBuffer = this.dataUrlToArrayBuffer(latestScreenshot);
17094
- const imageBufferSize = imageBuffer.byteLength;
17095
- // 构建配置的二进制结构
17096
- const configBuffer = this.buildBinaryConfig(config);
17097
- const configBufferSize = configBuffer.byteLength;
17098
- // 合并配置字节和图片字节(配置在前)
17099
- const combinedBuffer = this.combineBinaryData(configBuffer, imageBuffer);
17100
- const combinedBufferSize = combinedBuffer.byteLength;
17101
- // 打印大小信息
17102
- if (!this.options.silentMode) {
17103
- console.log('📸 [大小统计]');
17104
- console.log(` Base64 大小: ${base64Size} 字符`);
17105
- console.log(` 图片字节大小: ${(imageBufferSize / 1024).toFixed(2)} KB (${imageBufferSize} 字节)`);
17106
- console.log(` 配置字节大小: ${configBufferSize} 字节`);
17107
- console.log(` 拼接后总大小: ${(combinedBufferSize / 1024).toFixed(2)} KB (${combinedBufferSize} 字节)`);
17108
- }
17109
- // 发送二进制数据到 iframe
17110
- if (this.sendToIframeCallback) {
17111
- const message = {
17112
- type: 'screenshotBinary',
17113
- data: combinedBuffer
17114
- };
17115
- this.sendToIframeCallback(message);
17151
+ // 如果启用了压缩,等待 Worker 压缩完成后在 onmessage 中自动发送
17152
+ // 如果没有启用压缩,立即发送原始截图
17153
+ if (!this.options.compress) {
17154
+ // 没有压缩,直接发送原始截图
17155
+ const latestScreenshot = this.getLatestScreenshot();
17156
+ if (latestScreenshot) {
17157
+ try {
17158
+ // 计算 base64 大小
17159
+ const base64Data = latestScreenshot.split(',')[1] || '';
17160
+ const base64Size = base64Data.length;
17161
+ // 将截图转换为 ArrayBuffer
17162
+ const imageBuffer = this.dataUrlToArrayBuffer(latestScreenshot);
17163
+ const imageBufferSize = imageBuffer.byteLength;
17164
+ // 构建配置的二进制结构
17165
+ const configBuffer = this.buildBinaryConfig(config);
17166
+ const configBufferSize = configBuffer.byteLength;
17167
+ // 合并配置字节和图片字节(配置在前)
17168
+ const combinedBuffer = this.combineBinaryData(configBuffer, imageBuffer);
17169
+ const combinedBufferSize = combinedBuffer.byteLength;
17170
+ // 打印大小信息
17116
17171
  if (!this.options.silentMode) {
17117
- console.log('📸 [iframe] ✅ 二进制数据已发送到 iframe');
17172
+ console.log('📸 [大小统计]');
17173
+ console.log(` Base64 大小: ${base64Size} 字符`);
17174
+ console.log(` 图片字节大小: ${(imageBufferSize / 1024).toFixed(2)} KB (${imageBufferSize} 字节)`);
17175
+ console.log(` 配置字节大小: ${configBufferSize} 字节`);
17176
+ console.log(` 拼接后总大小: ${(combinedBufferSize / 1024).toFixed(2)} KB (${combinedBufferSize} 字节)`);
17177
+ }
17178
+ // 发送二进制数据到 iframe
17179
+ if (this.sendToIframeCallback) {
17180
+ const message = {
17181
+ type: 'screenshotBinary',
17182
+ data: combinedBuffer
17183
+ };
17184
+ this.sendToIframeCallback(message);
17185
+ if (!this.options.silentMode) {
17186
+ console.log('📸 [iframe] ✅ 二进制数据已发送到 iframe');
17187
+ }
17188
+ }
17189
+ else {
17190
+ console.error('📸 [iframe] ❌ 无法发送二进制数据:未提供发送消息的回调函数');
17118
17191
  }
17119
17192
  }
17120
- else {
17121
- console.error('📸 [iframe] ❌ 无法发送二进制数据:未提供发送消息的回调函数');
17193
+ catch (error) {
17194
+ console.error('📸 [iframe] ❌ 处理二进制数据失败:', error);
17195
+ this.uploadError = error instanceof Error ? error.message : String(error);
17122
17196
  }
17123
17197
  }
17124
- catch (error) {
17125
- console.error('📸 [iframe] ❌ 处理二进制数据失败:', error);
17126
- this.uploadError = error instanceof Error ? error.message : String(error);
17198
+ else {
17199
+ if (!this.options.silentMode) {
17200
+ console.warn('📸 [iframe] 截图完成但未找到截图数据');
17201
+ }
17127
17202
  }
17128
17203
  }
17129
17204
  else {
17205
+ // 启用了压缩,等待 Worker 压缩完成后在 onmessage 中自动发送
17130
17206
  if (!this.options.silentMode) {
17131
- console.warn('📸 [iframe] 截图完成但未找到截图数据');
17207
+ console.log('📸 [iframe] 等待 Worker 压缩完成后发送到 iframe...');
17132
17208
  }
17133
17209
  }
17134
17210
  }
@@ -17144,6 +17220,48 @@ class ScreenshotManager {
17144
17220
  getLatestScreenshot() {
17145
17221
  return this.screenshotHistory[this.screenshotHistory.length - 1] || null;
17146
17222
  }
17223
+ /**
17224
+ * 发送压缩后的截图到 iframe
17225
+ */
17226
+ sendCompressedScreenshotToIframe(dataUrl) {
17227
+ if (!this.currentBinaryConfig || !this.sendToIframeCallback) {
17228
+ return;
17229
+ }
17230
+ try {
17231
+ // 计算 base64 大小
17232
+ const base64Data = dataUrl.split(',')[1] || '';
17233
+ const base64Size = base64Data.length;
17234
+ // 将截图转换为 ArrayBuffer
17235
+ const imageBuffer = this.dataUrlToArrayBuffer(dataUrl);
17236
+ const imageBufferSize = imageBuffer.byteLength;
17237
+ // 构建配置的二进制结构
17238
+ const configBuffer = this.buildBinaryConfig(this.currentBinaryConfig);
17239
+ const configBufferSize = configBuffer.byteLength;
17240
+ // 合并配置字节和图片字节(配置在前)
17241
+ const combinedBuffer = this.combineBinaryData(configBuffer, imageBuffer);
17242
+ const combinedBufferSize = combinedBuffer.byteLength;
17243
+ // 打印大小信息
17244
+ if (!this.options.silentMode) {
17245
+ console.log('📸 [压缩后-大小统计]');
17246
+ console.log(` Base64 大小: ${base64Size} 字符`);
17247
+ console.log(` 图片字节大小: ${(imageBufferSize / 1024).toFixed(2)} KB (${imageBufferSize} 字节)`);
17248
+ console.log(` 配置字节大小: ${configBufferSize} 字节`);
17249
+ console.log(` 拼接后总大小: ${(combinedBufferSize / 1024).toFixed(2)} KB (${combinedBufferSize} 字节)`);
17250
+ }
17251
+ // 发送二进制数据到 iframe
17252
+ const message = {
17253
+ type: 'screenshotBinary',
17254
+ data: combinedBuffer
17255
+ };
17256
+ this.sendToIframeCallback(message);
17257
+ if (!this.options.silentMode) {
17258
+ console.log('📸 [压缩后] ✅ 二进制数据已发送到 iframe');
17259
+ }
17260
+ }
17261
+ catch (error) {
17262
+ console.error('📸 [压缩后] ❌ 处理二进制数据失败:', error);
17263
+ }
17264
+ }
17147
17265
  /**
17148
17266
  * 启用/禁用截图功能
17149
17267
  */