优化代码调用输出效果

This commit is contained in:
Vinlic 2024-03-21 11:51:26 +08:00
parent 5158dee32c
commit 888455b56a

View File

@ -479,7 +479,8 @@ async function receiveStream(stream: any): Promise<any> {
created: util.unixTimestamp() created: util.unixTimestamp()
}; };
let toolCall = false; let toolCall = false;
// let codeGenerating = false; let codeGenerating = false;
let codeTemp = '';
let lastExecutionOutput = ''; let lastExecutionOutput = '';
let textOffset = 0; let textOffset = 0;
const parser = createParser(event => { const parser = createParser(event => {
@ -518,18 +519,24 @@ async function receiveStream(stream: any): Promise<any> {
toolCall = true; toolCall = true;
return innerStr + imageText; return innerStr + imageText;
} }
// else if(type == 'code' && partStatus == 'init' && !codeGenerating) { else if(type == 'code' && partStatus == 'init') {
// codeGenerating = true; let codeHead = '';
// const label = '代码生成中...\n'; if(!codeGenerating) {
// textOffset += label.length; codeGenerating = true;
// return innerStr + label; codeHead = '```python\n';
// } }
// else if(type == 'code' && partStatus == 'finish' && codeGenerating) { const chunk = code.substring(codeTemp.length, code.length);
// codeGenerating = false; codeTemp += chunk;
// const label = '代码执行中...\n'; textOffset += codeHead.length + chunk.length;
// textOffset += label.length; return innerStr + codeHead + chunk;
// return innerStr + label; }
// } else if(type == 'code' && partStatus == 'finish' && codeGenerating) {
const codeFooter = '\n```';
codeGenerating = false;
codeTemp = '';
textOffset += codeFooter.length;
return innerStr + codeFooter;
}
else if(type == 'execution_output' && _.isString(content) && partStatus == 'done' && lastExecutionOutput != content) { else if(type == 'execution_output' && _.isString(content) && partStatus == 'done' && lastExecutionOutput != content) {
lastExecutionOutput = content; lastExecutionOutput = content;
const _content = content.replace(/^\n/, ''); const _content = content.replace(/^\n/, '');
@ -576,6 +583,7 @@ function createTransStream(stream: any, endCallback?: Function) {
let content = ''; let content = '';
let toolCall = false; let toolCall = false;
let codeGenerating = false; let codeGenerating = false;
let codeTemp = '';
let lastExecutionOutput = ''; let lastExecutionOutput = '';
let textOffset = 0; let textOffset = 0;
!transStream.closed && transStream.write(`data: ${JSON.stringify({ !transStream.closed && transStream.write(`data: ${JSON.stringify({
@ -621,17 +629,23 @@ function createTransStream(stream: any, endCallback?: Function) {
toolCall = true; toolCall = true;
return innerStr + imageText; return innerStr + imageText;
} }
else if(type == 'code' && partStatus == 'init' && !codeGenerating) { else if(type == 'code' && partStatus == 'init') {
codeGenerating = true; let codeHead = '';
const label = '代码生成中...\n'; if(!codeGenerating) {
textOffset += label.length; codeGenerating = true;
return innerStr + label; codeHead = '```python\n';
}
const chunk = code.substring(codeTemp.length, code.length);
codeTemp += chunk;
textOffset += codeHead.length + chunk.length;
return innerStr + codeHead + chunk;
} }
else if(type == 'code' && partStatus == 'finish' && codeGenerating) { else if(type == 'code' && partStatus == 'finish' && codeGenerating) {
const codeFooter = '\n```';
codeGenerating = false; codeGenerating = false;
const label = '代码执行中...\n'; codeTemp = '';
textOffset += label.length; textOffset += codeFooter.length;
return innerStr + label; return innerStr + codeFooter;
} }
else if(type == 'execution_output' && _.isString(content) && partStatus == 'done' && lastExecutionOutput != content) { else if(type == 'execution_output' && _.isString(content) && partStatus == 'done' && lastExecutionOutput != content) {
lastExecutionOutput = content; lastExecutionOutput = content;