|
65 | 65 | }
|
66 | 66 |
|
67 | 67 | function storeMessages() {
|
68 |
| -
|
69 | 68 | localStorage.setItem(
|
70 | 69 | LOCAL_STORAGE_KEY.STORAGE_CHAT_KEY,
|
71 |
| - JSON.stringify(chatMessages) |
| 70 | + JSON.stringify(chatMessages), |
72 | 71 | );
|
73 | 72 | }
|
74 | 73 |
|
| 74 | + function decodeEscapedBytes(str: string): string { |
| 75 | + const byteArray = str |
| 76 | + .split("\\x") |
| 77 | + .slice(1) |
| 78 | + .map((byte) => parseInt(byte, 16)); |
| 79 | + return new TextDecoder("utf-8").decode(new Uint8Array(byteArray)); |
| 80 | + } |
| 81 | +
|
| 82 | + function decodeUnicode(str: string): string { |
| 83 | + return str.replace(/\\u[\dA-Fa-f]{4}/g, (match) => { |
| 84 | + return String.fromCharCode(parseInt(match.replace(/\\u/g, ""), 16)); |
| 85 | + }); |
| 86 | + } |
| 87 | +
|
75 | 88 | const callTextStream = async (query: string, startSendTime: number) => {
|
76 | 89 | const eventSource = await fetchTextStream(query, knowledge_1);
|
77 | 90 |
|
78 | 91 | eventSource.addEventListener("message", (e: any) => {
|
79 | 92 | let Msg = e.data;
|
80 | 93 | if (Msg.startsWith("b")) {
|
81 |
| - const currentMsg = Msg.slice(2, -1); |
| 94 | + let currentMsg = Msg.slice(2, -1); |
| 95 | +
|
| 96 | + if (/\\x[\dA-Fa-f]{2}/.test(currentMsg)) { |
| 97 | + currentMsg = decodeEscapedBytes(currentMsg); |
| 98 | + } else if (/\\u[\dA-Fa-f]{4}/.test(currentMsg)) { |
| 99 | + currentMsg = decodeUnicode(currentMsg); |
| 100 | + } |
| 101 | + if (currentMsg !== "</s>") { |
| 102 | + currentMsg = currentMsg.replace(/\\n/g, "\n"); |
| 103 | + } |
| 104 | +
|
82 | 105 | if (chatMessages[chatMessages.length - 1].role == MessageRole.User) {
|
83 | 106 | chatMessages = [
|
84 | 107 | ...chatMessages,
|
|
99 | 122 |
|
100 | 123 | loading = false;
|
101 | 124 | let totalTime = parseFloat(
|
102 |
| - ((getCurrentTimeStamp() - startTime) / 1000).toFixed(2) |
| 125 | + ((getCurrentTimeStamp() - startTime) / 1000).toFixed(2), |
103 | 126 | );
|
104 | 127 |
|
105 | 128 | if (chatMessages.length - 1 !== -1) {
|
|
0 commit comments