修复调用tool之前文本导致后续内容混乱问题

This commit is contained in:
Vinlic 2024-03-21 13:17:28 +08:00
parent 0a9373ed94
commit 8b7ab6a252

View File

@ -480,6 +480,7 @@ async function receiveStream(stream: any): Promise<any> {
}; };
let toolCall = false; let toolCall = false;
let codeGenerating = false; let codeGenerating = false;
let textChunkLength = 0;
let codeTemp = ''; let codeTemp = '';
let lastExecutionOutput = ''; let lastExecutionOutput = '';
let textOffset = 0; let textOffset = 0;
@ -499,12 +500,19 @@ async function receiveStream(stream: any): Promise<any> {
return str; return str;
const partText = content.reduce((innerStr, value) => { const partText = content.reduce((innerStr, value) => {
const { status: partStatus, type, text, image, code, content } = value; const { status: partStatus, type, text, image, code, content } = value;
if(partStatus == 'init' && textChunkLength > 0) {
textOffset += textChunkLength + 1;
textChunkLength = 0;
innerStr += '\n';
}
if(type == 'text') { if(type == 'text') {
if(toolCall) { if(toolCall) {
innerStr += '\n'; innerStr += '\n';
textOffset++; textOffset++;
toolCall = false; toolCall = false;
} }
if(partStatus == 'finish')
textChunkLength = text.length;
return innerStr + text; return innerStr + text;
} }
else if(type == 'quote_result' && status == 'finish' && meta_data && _.isArray(meta_data.metadata_list)) { else if(type == 'quote_result' && status == 'finish' && meta_data && _.isArray(meta_data.metadata_list)) {
@ -531,7 +539,7 @@ async function receiveStream(stream: any): Promise<any> {
return innerStr + codeHead + chunk; return innerStr + codeHead + chunk;
} }
else if(type == 'code' && partStatus == 'finish' && codeGenerating) { else if(type == 'code' && partStatus == 'finish' && codeGenerating) {
const codeFooter = '\n```'; const codeFooter = '```\n';
codeGenerating = false; codeGenerating = false;
codeTemp = ''; codeTemp = '';
textOffset += codeFooter.length; textOffset += codeFooter.length;
@ -583,6 +591,7 @@ function createTransStream(stream: any, endCallback?: Function) {
let content = ''; let content = '';
let toolCall = false; let toolCall = false;
let codeGenerating = false; let codeGenerating = false;
let textChunkLength = 0;
let codeTemp = ''; let codeTemp = '';
let lastExecutionOutput = ''; let lastExecutionOutput = '';
let textOffset = 0; let textOffset = 0;
@ -609,13 +618,20 @@ function createTransStream(stream: any, endCallback?: Function) {
return str; return str;
const partText = content.reduce((innerStr, value) => { const partText = content.reduce((innerStr, value) => {
const { status: partStatus, type, text, image, code, content } = value; const { status: partStatus, type, text, image, code, content } = value;
if(partStatus == 'init' && textChunkLength > 0) {
textOffset += textChunkLength + 1;
textChunkLength = 0;
innerStr += '\n';
}
if(type == 'text') { if(type == 'text') {
if(toolCall) { if(toolCall) {
innerStr += '\n'; innerStr += '\n';
textOffset++; textOffset++;
toolCall = false; toolCall = false;
} }
return innerStr + text.replace(/【\d+†source】/g, ''); if(partStatus == 'finish')
textChunkLength = text.length;
return innerStr + text;
} }
else if(type == 'quote_result' && status == 'finish' && meta_data && _.isArray(meta_data.metadata_list)) { else if(type == 'quote_result' && status == 'finish' && meta_data && _.isArray(meta_data.metadata_list)) {
const searchText = meta_data.metadata_list.reduce((meta, v) => meta + `检索 ${v.title}(${v.url}) ...`, '') + '\n'; const searchText = meta_data.metadata_list.reduce((meta, v) => meta + `检索 ${v.title}(${v.url}) ...`, '') + '\n';
@ -641,7 +657,7 @@ function createTransStream(stream: any, endCallback?: Function) {
return innerStr + codeHead + chunk; return innerStr + codeHead + chunk;
} }
else if(type == 'code' && partStatus == 'finish' && codeGenerating) { else if(type == 'code' && partStatus == 'finish' && codeGenerating) {
const codeFooter = '\n```'; const codeFooter = '```\n';
codeGenerating = false; codeGenerating = false;
codeTemp = ''; codeTemp = '';
textOffset += codeFooter.length; textOffset += codeFooter.length;