Skip to content

Commit 84357ef

Browse files
committed
在缓冲区处理UTF-8字符合并,确认修复乱码
1 parent c4161df commit 84357ef

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "doubao-free-api",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Doubao Free API Server",
55
"type": "module",
66
"main": "dist/index.js",

src/api/controllers/chat.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ function checkResult(result: AxiosResponse) {
587587
* @param stream 消息流
588588
*/
589589
async function receiveStream(stream: any): Promise<any> {
590+
let temp = Buffer.from('');
590591
return new Promise((resolve, reject) => {
591592
// 消息初始化
592593
const data = {
@@ -635,18 +636,28 @@ async function receiveStream(stream: any): Promise<any> {
635636
if (!message || ![2001, 2008].includes(message.content_type))
636637
return;
637638
const content = JSON.parse(message.content);
638-
if (content.text) {
639-
const text = content.text;
640-
const exceptCharIndex = text.indexOf("�");
641-
data.choices[0].message.content += text.substring(0, exceptCharIndex == -1 ? text.length : exceptCharIndex);
642-
}
639+
if (content.text)
640+
data.choices[0].message.content += content.text;
643641
} catch (err) {
644642
logger.error(err);
645643
reject(err);
646644
}
647645
});
648646
// 将流数据喂给SSE转换器
649-
stream.on("data", (buffer) => parser.feed(buffer.toString()));
647+
stream.on("data", (buffer) => {
648+
// 检查buffer是否以完整UTF8字符结尾
649+
if (buffer.toString().indexOf('�') != -1) {
650+
// 如果不完整则累积buffer直到收到完整字符
651+
temp = Buffer.concat([temp, buffer]);
652+
return;
653+
}
654+
// 将之前累积的不完整buffer拼接
655+
if (temp.length > 0) {
656+
buffer = Buffer.concat([temp, buffer]);
657+
temp = Buffer.from('');
658+
}
659+
parser.feed(buffer.toString());
660+
});
650661
stream.once("error", (err) => reject(err));
651662
stream.once("close", () => resolve(data));
652663
});
@@ -662,6 +673,7 @@ async function receiveStream(stream: any): Promise<any> {
662673
*/
663674
function createTransStream(stream: any, endCallback?: Function) {
664675
let convId = "";
676+
let temp = Buffer.from('');
665677
// 消息创建时间
666678
const created = util.unixTimestamp();
667679
// 创建转换流
@@ -710,8 +722,9 @@ function createTransStream(stream: any, endCallback?: Function) {
710722
endCallback && endCallback(convId);
711723
return;
712724
}
713-
if (rawResult.event_type != 2001)
725+
if (rawResult.event_type != 2001) {
714726
return;
727+
}
715728
const result = _.attempt(() => JSON.parse(rawResult.event_data));
716729
if (_.isError(result))
717730
throw new Error(`Stream response invalid: ${rawResult.event_data}`);
@@ -740,17 +753,14 @@ function createTransStream(stream: any, endCallback?: Function) {
740753
return;
741754
const content = JSON.parse(message.content);
742755
if (content.text) {
743-
const text = content.text;
744-
const exceptCharIndex = text.indexOf("�");
745-
const chunk = text.substring(0, exceptCharIndex == -1 ? text.length : exceptCharIndex);
746756
transStream.write(`data: ${JSON.stringify({
747757
id: convId,
748758
model: MODEL_NAME,
749759
object: "chat.completion.chunk",
750760
choices: [
751761
{
752762
index: 0,
753-
delta: { role: "assistant", content: chunk },
763+
delta: { role: "assistant", content: content.text },
754764
finish_reason: null,
755765
},
756766
],
@@ -763,7 +773,20 @@ function createTransStream(stream: any, endCallback?: Function) {
763773
}
764774
});
765775
// 将流数据喂给SSE转换器
766-
stream.on("data", (buffer) => parser.feed(buffer.toString()));
776+
stream.on("data", (buffer) => {
777+
// 检查buffer是否以完整UTF8字符结尾
778+
if (buffer.toString().indexOf('�') != -1) {
779+
// 如果不完整则累积buffer直到收到完整字符
780+
temp = Buffer.concat([temp, buffer]);
781+
return;
782+
}
783+
// 将之前累积的不完整buffer拼接
784+
if (temp.length > 0) {
785+
buffer = Buffer.concat([temp, buffer]);
786+
temp = Buffer.from('');
787+
}
788+
parser.feed(buffer.toString());
789+
});
767790
stream.once(
768791
"error",
769792
() => !transStream.closed && transStream.end("data: [DONE]\n\n")

0 commit comments

Comments
 (0)