feat: 对话解析优化
This commit is contained in:
@@ -63,7 +63,7 @@ function agentChatStream(params, onChunk) {
|
|||||||
const base64 = uni.arrayBufferToBase64(res.data);
|
const base64 = uni.arrayBufferToBase64(res.data);
|
||||||
let data = '';
|
let data = '';
|
||||||
try {
|
try {
|
||||||
data = decodeURIComponent(escape(atob(base64)));
|
data = decodeURIComponent(escape(weAtob(base64)));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// 某些平台可能不支持 atob,可以直接用 base64
|
// 某些平台可能不支持 atob,可以直接用 base64
|
||||||
data = base64;
|
data = base64;
|
||||||
@@ -77,6 +77,59 @@ function agentChatStream(params, onChunk) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// window.atob兼容性处理
|
||||||
|
const weAtob = (string) => {
|
||||||
|
const b64re =
|
||||||
|
/^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/;
|
||||||
|
const b64 =
|
||||||
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
||||||
|
|
||||||
|
// 去除空白字符
|
||||||
|
string = String(string).replace(/[\t\n\f\r ]+/g, '');
|
||||||
|
|
||||||
|
// 验证 Base64 编码
|
||||||
|
if (!b64re.test(string)) {
|
||||||
|
throw new TypeError(
|
||||||
|
// eslint-disable-next-line quotes
|
||||||
|
"Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 填充字符
|
||||||
|
string += '=='.slice(2 - (string.length & 3));
|
||||||
|
|
||||||
|
let bitmap,
|
||||||
|
result = '',
|
||||||
|
r1,
|
||||||
|
r2,
|
||||||
|
i = 0;
|
||||||
|
|
||||||
|
for (; i < string.length;) {
|
||||||
|
bitmap =
|
||||||
|
(b64.indexOf(string.charAt(i++)) << 18) |
|
||||||
|
(b64.indexOf(string.charAt(i++)) << 12) |
|
||||||
|
((r1 = b64.indexOf(string.charAt(i++))) << 6) |
|
||||||
|
(r2 = b64.indexOf(string.charAt(i++)));
|
||||||
|
|
||||||
|
if (r1 === 64) {
|
||||||
|
result += String.fromCharCode((bitmap >> 16) & 255);
|
||||||
|
} else if (r2 === 64) {
|
||||||
|
result += String.fromCharCode(
|
||||||
|
(bitmap >> 16) & 255,
|
||||||
|
(bitmap >> 8) & 255
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
result += String.fromCharCode(
|
||||||
|
(bitmap >> 16) & 255,
|
||||||
|
(bitmap >> 8) & 255,
|
||||||
|
bitmap & 255
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// 解析SSE分段数据
|
// 解析SSE分段数据
|
||||||
function parseSSEChunk(raw) {
|
function parseSSEChunk(raw) {
|
||||||
// 拆分为多段
|
// 拆分为多段
|
||||||
|
|||||||
Reference in New Issue
Block a user