mirror of
https://github.com/LLM-Red-Team/glm-free-api.git
synced 2024-12-23 01:19:22 +08:00
支持代码调用过程和结果展示
This commit is contained in:
parent
c7ee175c18
commit
5eae87e697
@ -479,6 +479,8 @@ async function receiveStream(stream: any): Promise<any> {
|
||||
created: util.unixTimestamp()
|
||||
};
|
||||
let toolCall = false;
|
||||
// let codeGenerating = false;
|
||||
let lastExecutionOutput = '';
|
||||
let textOffset = 0;
|
||||
const parser = createParser(event => {
|
||||
try {
|
||||
@ -495,7 +497,7 @@ async function receiveStream(stream: any): Promise<any> {
|
||||
if(!_.isArray(content))
|
||||
return str;
|
||||
const partText = content.reduce((innerStr, value) => {
|
||||
const { type, text, image } = value;
|
||||
const { status: partStatus, type, text, image, code, content } = value;
|
||||
if(type == 'text') {
|
||||
if(toolCall) {
|
||||
innerStr += '\n';
|
||||
@ -516,6 +518,24 @@ async function receiveStream(stream: any): Promise<any> {
|
||||
toolCall = true;
|
||||
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 str + partText;
|
||||
@ -555,6 +575,8 @@ function createTransStream(stream: any, endCallback?: Function) {
|
||||
const transStream = new PassThrough();
|
||||
let content = '';
|
||||
let toolCall = false;
|
||||
let codeGenerating = false;
|
||||
let lastExecutionOutput = '';
|
||||
let textOffset = 0;
|
||||
!transStream.closed && transStream.write(`data: ${JSON.stringify({
|
||||
id: '',
|
||||
@ -578,7 +600,7 @@ function createTransStream(stream: any, endCallback?: Function) {
|
||||
if(!_.isArray(content))
|
||||
return str;
|
||||
const partText = content.reduce((innerStr, value) => {
|
||||
const { type, text, image } = value;
|
||||
const { status: partStatus, type, text, image, code, content } = value;
|
||||
if(type == 'text') {
|
||||
if(toolCall) {
|
||||
innerStr += '\n';
|
||||
@ -599,6 +621,23 @@ function createTransStream(stream: any, endCallback?: Function) {
|
||||
toolCall = true;
|
||||
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 str + partText;
|
||||
|
Loading…
Reference in New Issue
Block a user