支持代码调用过程和结果展示

This commit is contained in:
Vinlic 2024-03-21 00:38:27 +08:00
parent c7ee175c18
commit 5eae87e697

View File

@ -479,6 +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 lastExecutionOutput = '';
let textOffset = 0; let textOffset = 0;
const parser = createParser(event => { const parser = createParser(event => {
try { try {
@ -495,7 +497,7 @@ async function receiveStream(stream: any): Promise<any> {
if(!_.isArray(content)) if(!_.isArray(content))
return str; return str;
const partText = content.reduce((innerStr, value) => { const partText = content.reduce((innerStr, value) => {
const { type, text, image } = value; const { status: partStatus, type, text, image, code, content } = value;
if(type == 'text') { if(type == 'text') {
if(toolCall) { if(toolCall) {
innerStr += '\n'; innerStr += '\n';
@ -516,6 +518,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) {
// codeGenerating = true;
// const label = '代码生成中...\n';
// textOffset += label.length;
// return innerStr + label;
// }
// else if(type == 'code' && partStatus == 'finish' && codeGenerating) {
// codeGenerating = false;
// const label = '代码执行中...\n';
// textOffset += label.length;
// return innerStr + label;
// }
else if(type == 'execution_output' && _.isString(content) && partStatus == 'done' && lastExecutionOutput != content) {
lastExecutionOutput = content;
const _content = content.replace(/^\n/, '');
textOffset += _content.length + 1;
return innerStr + _content + '\n';
}
return innerStr; return innerStr;
}, ''); }, '');
return str + partText; return str + partText;
@ -555,6 +575,8 @@ function createTransStream(stream: any, endCallback?: Function) {
const transStream = new PassThrough(); const transStream = new PassThrough();
let content = ''; let content = '';
let toolCall = false; let toolCall = false;
let codeGenerating = false;
let lastExecutionOutput = '';
let textOffset = 0; let textOffset = 0;
!transStream.closed && transStream.write(`data: ${JSON.stringify({ !transStream.closed && transStream.write(`data: ${JSON.stringify({
id: '', id: '',
@ -578,7 +600,7 @@ function createTransStream(stream: any, endCallback?: Function) {
if(!_.isArray(content)) if(!_.isArray(content))
return str; return str;
const partText = content.reduce((innerStr, value) => { const partText = content.reduce((innerStr, value) => {
const { type, text, image } = value; const { status: partStatus, type, text, image, code, content } = value;
if(type == 'text') { if(type == 'text') {
if(toolCall) { if(toolCall) {
innerStr += '\n'; innerStr += '\n';
@ -599,6 +621,23 @@ function createTransStream(stream: any, endCallback?: Function) {
toolCall = true; toolCall = true;
return innerStr + imageText; return innerStr + imageText;
} }
else if(type == 'code' && partStatus == 'init' && !codeGenerating) {
codeGenerating = true;
const label = '代码生成中...\n';
textOffset += label.length;
return innerStr + label;
}
else if(type == 'code' && partStatus == 'finish' && codeGenerating) {
codeGenerating = false;
const label = '代码执行中...\n';
textOffset += label.length;
return innerStr + label;
}
else if(type == 'execution_output' && _.isString(content) && partStatus == 'done' && lastExecutionOutput != content) {
lastExecutionOutput = content;
textOffset += content.length + 1;
return innerStr + content + '\n';
}
return innerStr; return innerStr;
}, ''); }, '');
return str + partText; return str + partText;