customer-chat-sdk 1.0.49 → 1.0.51

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.
@@ -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
@@ -17021,34 +17083,37 @@ class ScreenshotManager {
17021
17083
  return u8arr.buffer;
17022
17084
  }
17023
17085
  /**
17024
- * 构建二进制结构(按顺序:sign, type, topic, routingKey)
17025
- * sign: 8字节 (BigInt64)
17026
- * type: 1字节 (Uint8)
17027
- * topic: 8字节 (字符串,UTF-8编码,不足补0)
17028
- * routingKey: 8字节 (字符串,UTF-8编码,不足补0)
17086
+ * 构建二进制结构(按顺序:sign[9], type[1], topic[8], routingKey[8]
17087
+ * 总大小:9 + 1 + 8 + 8 = 26 字节
17029
17088
  */
17030
17089
  buildBinaryConfig(config) {
17031
- // 总大小:8 + 1 + 8 + 8 = 25 字节
17032
- const buffer = new ArrayBuffer(25);
17090
+ // 总大小:9 + 1 + 8 + 8 = 26 字节
17091
+ const buffer = new ArrayBuffer(26);
17033
17092
  const view = new DataView(buffer);
17034
17093
  const encoder = new TextEncoder();
17035
17094
  let offset = 0;
17036
- // sign: 8字节 (BigInt64)
17037
- // 将字符串转换为 BigInt(支持数字字符串,如 "1234567890")
17095
+ // sign: 9字节 (BigInt 或自定义编码)
17038
17096
  const signValue = typeof config.sign === 'string'
17039
17097
  ? BigInt(config.sign)
17040
17098
  : BigInt(String(config.sign));
17041
- view.setBigInt64(offset, signValue);
17042
- offset += 8;
17043
- // type: 1字节 (Uint8)
17099
+ // 我们只能直接写 8 字节(DataView 不支持 9字节整数),所以:
17100
+ // 若后端确实是9字节整型(非标准),可手动写入
17101
+ // 例如前导一个 0 再加上 BigInt64 部分
17102
+ const signBuffer = new Uint8Array(9);
17103
+ const tempView = new DataView(new ArrayBuffer(8));
17104
+ tempView.setBigInt64(0, signValue);
17105
+ signBuffer.set(new Uint8Array(tempView.buffer), 1); // 前面补一个 0
17106
+ new Uint8Array(buffer, offset, 9).set(signBuffer);
17107
+ offset += 9;
17108
+ // type: 1字节
17044
17109
  view.setUint8(offset, config.type);
17045
17110
  offset += 1;
17046
- // topic: 8字节 (字符串,UTF-8编码,不足补0)
17111
+ // topic: 8字节
17047
17112
  const topicBytes = encoder.encode(config.topic);
17048
17113
  const topicArray = new Uint8Array(buffer, offset, 8);
17049
17114
  topicArray.set(topicBytes.slice(0, 8));
17050
17115
  offset += 8;
17051
- // routingKey: 8字节 (字符串,UTF-8编码,不足补0)
17116
+ // routingKey: 8字节
17052
17117
  const routingKeyBytes = encoder.encode(config.routingKey);
17053
17118
  const routingKeyArray = new Uint8Array(buffer, offset, 8);
17054
17119
  routingKeyArray.set(routingKeyBytes.slice(0, 8));
@@ -17086,53 +17151,63 @@ class ScreenshotManager {
17086
17151
  if (success) {
17087
17152
  // 截图完成后,等待一小段时间确保数据已保存
17088
17153
  await new Promise(resolve => setTimeout(resolve, 100));
17089
- // 获取最新截图并转换为二进制
17090
- const latestScreenshot = this.getLatestScreenshot();
17091
- if (latestScreenshot) {
17092
- try {
17093
- // 计算 base64 大小
17094
- const base64Data = latestScreenshot.split(',')[1] || '';
17095
- const base64Size = base64Data.length;
17096
- // 将截图转换为 ArrayBuffer
17097
- const imageBuffer = this.dataUrlToArrayBuffer(latestScreenshot);
17098
- const imageBufferSize = imageBuffer.byteLength;
17099
- // 构建配置的二进制结构
17100
- const configBuffer = this.buildBinaryConfig(config);
17101
- const configBufferSize = configBuffer.byteLength;
17102
- // 合并配置字节和图片字节(配置在前)
17103
- const combinedBuffer = this.combineBinaryData(configBuffer, imageBuffer);
17104
- const combinedBufferSize = combinedBuffer.byteLength;
17105
- // 打印大小信息
17106
- if (!this.options.silentMode) {
17107
- console.log('📸 [大小统计]');
17108
- console.log(` Base64 大小: ${base64Size} 字符`);
17109
- console.log(` 图片字节大小: ${(imageBufferSize / 1024).toFixed(2)} KB (${imageBufferSize} 字节)`);
17110
- console.log(` 配置字节大小: ${configBufferSize} 字节`);
17111
- console.log(` 拼接后总大小: ${(combinedBufferSize / 1024).toFixed(2)} KB (${combinedBufferSize} 字节)`);
17112
- }
17113
- // 发送二进制数据到 iframe
17114
- if (this.sendToIframeCallback) {
17115
- const message = {
17116
- type: 'screenshotBinary',
17117
- data: combinedBuffer
17118
- };
17119
- this.sendToIframeCallback(message);
17154
+ // 如果启用了压缩,等待 Worker 压缩完成后在 onmessage 中自动发送
17155
+ // 如果没有启用压缩,立即发送原始截图
17156
+ if (!this.options.compress) {
17157
+ // 没有压缩,直接发送原始截图
17158
+ const latestScreenshot = this.getLatestScreenshot();
17159
+ if (latestScreenshot) {
17160
+ try {
17161
+ // 计算 base64 大小
17162
+ const base64Data = latestScreenshot.split(',')[1] || '';
17163
+ const base64Size = base64Data.length;
17164
+ // 将截图转换为 ArrayBuffer
17165
+ const imageBuffer = this.dataUrlToArrayBuffer(latestScreenshot);
17166
+ const imageBufferSize = imageBuffer.byteLength;
17167
+ // 构建配置的二进制结构
17168
+ const configBuffer = this.buildBinaryConfig(config);
17169
+ const configBufferSize = configBuffer.byteLength;
17170
+ // 合并配置字节和图片字节(配置在前)
17171
+ const combinedBuffer = this.combineBinaryData(configBuffer, imageBuffer);
17172
+ const combinedBufferSize = combinedBuffer.byteLength;
17173
+ // 打印大小信息
17120
17174
  if (!this.options.silentMode) {
17121
- console.log('📸 [iframe] ✅ 二进制数据已发送到 iframe');
17175
+ console.log('📸 [大小统计]');
17176
+ console.log(` Base64 大小: ${base64Size} 字符`);
17177
+ console.log(` 图片字节大小: ${(imageBufferSize / 1024).toFixed(2)} KB (${imageBufferSize} 字节)`);
17178
+ console.log(` 配置字节大小: ${configBufferSize} 字节`);
17179
+ console.log(` 拼接后总大小: ${(combinedBufferSize / 1024).toFixed(2)} KB (${combinedBufferSize} 字节)`);
17180
+ }
17181
+ // 发送二进制数据到 iframe
17182
+ if (this.sendToIframeCallback) {
17183
+ const message = {
17184
+ type: 'screenshotBinary',
17185
+ data: combinedBuffer
17186
+ };
17187
+ this.sendToIframeCallback(message);
17188
+ if (!this.options.silentMode) {
17189
+ console.log('📸 [iframe] ✅ 二进制数据已发送到 iframe');
17190
+ }
17191
+ }
17192
+ else {
17193
+ console.error('📸 [iframe] ❌ 无法发送二进制数据:未提供发送消息的回调函数');
17122
17194
  }
17123
17195
  }
17124
- else {
17125
- console.error('📸 [iframe] ❌ 无法发送二进制数据:未提供发送消息的回调函数');
17196
+ catch (error) {
17197
+ console.error('📸 [iframe] ❌ 处理二进制数据失败:', error);
17198
+ this.uploadError = error instanceof Error ? error.message : String(error);
17126
17199
  }
17127
17200
  }
17128
- catch (error) {
17129
- console.error('📸 [iframe] ❌ 处理二进制数据失败:', error);
17130
- this.uploadError = error instanceof Error ? error.message : String(error);
17201
+ else {
17202
+ if (!this.options.silentMode) {
17203
+ console.warn('📸 [iframe] 截图完成但未找到截图数据');
17204
+ }
17131
17205
  }
17132
17206
  }
17133
17207
  else {
17208
+ // 启用了压缩,等待 Worker 压缩完成后在 onmessage 中自动发送
17134
17209
  if (!this.options.silentMode) {
17135
- console.warn('📸 [iframe] 截图完成但未找到截图数据');
17210
+ console.log('📸 [iframe] 等待 Worker 压缩完成后发送到 iframe...');
17136
17211
  }
17137
17212
  }
17138
17213
  }
@@ -17148,6 +17223,48 @@ class ScreenshotManager {
17148
17223
  getLatestScreenshot() {
17149
17224
  return this.screenshotHistory[this.screenshotHistory.length - 1] || null;
17150
17225
  }
17226
+ /**
17227
+ * 发送压缩后的截图到 iframe
17228
+ */
17229
+ sendCompressedScreenshotToIframe(dataUrl) {
17230
+ if (!this.currentBinaryConfig || !this.sendToIframeCallback) {
17231
+ return;
17232
+ }
17233
+ try {
17234
+ // 计算 base64 大小
17235
+ const base64Data = dataUrl.split(',')[1] || '';
17236
+ const base64Size = base64Data.length;
17237
+ // 将截图转换为 ArrayBuffer
17238
+ const imageBuffer = this.dataUrlToArrayBuffer(dataUrl);
17239
+ const imageBufferSize = imageBuffer.byteLength;
17240
+ // 构建配置的二进制结构
17241
+ const configBuffer = this.buildBinaryConfig(this.currentBinaryConfig);
17242
+ const configBufferSize = configBuffer.byteLength;
17243
+ // 合并配置字节和图片字节(配置在前)
17244
+ const combinedBuffer = this.combineBinaryData(configBuffer, imageBuffer);
17245
+ const combinedBufferSize = combinedBuffer.byteLength;
17246
+ // 打印大小信息
17247
+ if (!this.options.silentMode) {
17248
+ console.log('📸 [压缩后-大小统计]');
17249
+ console.log(` Base64 大小: ${base64Size} 字符`);
17250
+ console.log(` 图片字节大小: ${(imageBufferSize / 1024).toFixed(2)} KB (${imageBufferSize} 字节)`);
17251
+ console.log(` 配置字节大小: ${configBufferSize} 字节`);
17252
+ console.log(` 拼接后总大小: ${(combinedBufferSize / 1024).toFixed(2)} KB (${combinedBufferSize} 字节)`);
17253
+ }
17254
+ // 发送二进制数据到 iframe
17255
+ const message = {
17256
+ type: 'screenshotBinary',
17257
+ data: combinedBuffer
17258
+ };
17259
+ this.sendToIframeCallback(message);
17260
+ if (!this.options.silentMode) {
17261
+ console.log('📸 [压缩后] ✅ 二进制数据已发送到 iframe');
17262
+ }
17263
+ }
17264
+ catch (error) {
17265
+ console.error('📸 [压缩后] ❌ 处理二进制数据失败:', error);
17266
+ }
17267
+ }
17151
17268
  /**
17152
17269
  * 启用/禁用截图功能
17153
17270
  */