fix escaping
This commit is contained in:
@@ -789,6 +789,8 @@ class YouProvider {
|
|||||||
}, traceId);
|
}, traceId);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 缓存
|
||||||
|
let buffer = '';
|
||||||
const self = this;
|
const self = this;
|
||||||
page.exposeFunction("callback" + traceId, async (event, data) => {
|
page.exposeFunction("callback" + traceId, async (event, data) => {
|
||||||
if (isEnding) return;
|
if (isEnding) return;
|
||||||
@@ -797,9 +799,14 @@ class YouProvider {
|
|||||||
case "youChatToken":
|
case "youChatToken":
|
||||||
data = JSON.parse(data);
|
data = JSON.parse(data);
|
||||||
let tokenContent = data.youChatToken;
|
let tokenContent = data.youChatToken;
|
||||||
|
// 将新接收到的内容添加到缓存中
|
||||||
// 对接收到的内容进行转义处理
|
buffer += tokenContent;
|
||||||
tokenContent = unescapeContent(tokenContent);
|
if (buffer.endsWith('\\') && !buffer.endsWith('\\\\')) {
|
||||||
|
// 等待下一个字符
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let processedContent = unescapeContent(buffer);
|
||||||
|
buffer = '';
|
||||||
|
|
||||||
if (!responseStarted) {
|
if (!responseStarted) {
|
||||||
responseStarted = true;
|
responseStarted = true;
|
||||||
@@ -812,7 +819,7 @@ class YouProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 检测 'unusual query volume'
|
// 检测 'unusual query volume'
|
||||||
if (tokenContent.includes('unusual query volume')) {
|
if (processedContent.includes('unusual query volume')) {
|
||||||
if (self.isRotationEnabled) {
|
if (self.isRotationEnabled) {
|
||||||
self.modeStatus[self.currentMode] = false;
|
self.modeStatus[self.currentMode] = false;
|
||||||
|
|
||||||
@@ -826,17 +833,17 @@ class YouProvider {
|
|||||||
isEnding = true;
|
isEnding = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
process.stdout.write(tokenContent);
|
process.stdout.write(processedContent);
|
||||||
accumulatedResponse += tokenContent;
|
accumulatedResponse += processedContent;
|
||||||
|
|
||||||
if (Date.now() - startTime >= 20000) {
|
if (Date.now() - startTime >= 20000) {
|
||||||
responseAfter20Seconds += tokenContent;
|
responseAfter20Seconds += processedContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stream) {
|
if (stream) {
|
||||||
emitter.emit("completion", traceId, tokenContent);
|
emitter.emit("completion", traceId, processedContent);
|
||||||
} else {
|
} else {
|
||||||
finalResponse += tokenContent;
|
finalResponse += processedContent;
|
||||||
}
|
}
|
||||||
// 只在启用自定义终止符后,且只检查20秒后的响应
|
// 只在启用自定义终止符后,且只检查20秒后的响应
|
||||||
if (customEndMarkerEnabled && customEndMarker && checkEndMarker(responseAfter20Seconds, customEndMarker)) {
|
if (customEndMarkerEnabled && customEndMarker && checkEndMarker(responseAfter20Seconds, customEndMarker)) {
|
||||||
@@ -1103,20 +1110,16 @@ class YouProvider {
|
|||||||
export default YouProvider;
|
export default YouProvider;
|
||||||
|
|
||||||
function unescapeContent(content) {
|
function unescapeContent(content) {
|
||||||
try {
|
// 将 \" 替换为 "
|
||||||
return JSON.parse(`"${content.replace(/"/g, '\\"')}"`);
|
content = content.replace(/\\"/g, '"');
|
||||||
} catch (e) {
|
|
||||||
// 将 \" 替换为 "
|
|
||||||
content = content.replace(/\\"/g, '"');
|
|
||||||
|
|
||||||
content = content.replace(/\\n/g, '');
|
content = content.replace(/\\n/g, '');
|
||||||
|
|
||||||
// 将 \r 替换为空字符
|
// 将 \r 替换为空字符
|
||||||
content = content.replace(/\\r/g, '');
|
content = content.replace(/\\r/g, '');
|
||||||
|
|
||||||
// 将 「 和 」 替换为 "
|
// 将 「 和 」 替换为 "
|
||||||
content = content.replace(/[「」]/g, '"');
|
content = content.replace(/[「」]/g, '"');
|
||||||
|
|
||||||
return content;
|
return content;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user