|
|
|
@ -158,18 +158,27 @@ export class MessageHandler {
|
|
|
|
this.provider._postMessage({command: 'hideLoading'});
|
|
|
|
this.provider._postMessage({command: 'hideLoading'});
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 如果是其他错误,重新抛出
|
|
|
|
// 如果是网络连接中断错误,我们仍然处理已接收的数据
|
|
|
|
throw readError;
|
|
|
|
if (readError.message.includes('terminated') || readError.message.includes('other side closed') || readError.name === 'TypeError') {
|
|
|
|
|
|
|
|
// 继续处理已接收的数据
|
|
|
|
|
|
|
|
console.log("网络连接中断,但将继续处理已接收的数据");
|
|
|
|
|
|
|
|
aiMessage += "```"
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 如果是其他错误,重新抛出
|
|
|
|
|
|
|
|
throw readError;
|
|
|
|
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
// 清理reader引用
|
|
|
|
// 清理reader引用
|
|
|
|
this.currentReader = null;
|
|
|
|
this.currentReader = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// console.log("aiMessage:",aiMessage)
|
|
|
|
// 流传输完成后,更新会话历史
|
|
|
|
// 流传输完成后,更新会话历史
|
|
|
|
currentSessionHistory = [...currentSessionHistory, {role: 'assistant', content: aiMessage}];
|
|
|
|
currentSessionHistory = [...currentSessionHistory, {role: 'assistant', content: aiMessage}];
|
|
|
|
|
|
|
|
|
|
|
|
// 解析AI响应中的多个代码块并生成独立的codeDiff信息
|
|
|
|
// 解析AI响应中的多个代码块并生成独立的codeDiff信息
|
|
|
|
const codeBlocks = aiMessage.match(/```[\s\S]*?```/g) || [];
|
|
|
|
const codeBlocks = aiMessage.match(/```[\s\S]*?```/g) || [];
|
|
|
|
|
|
|
|
// console.log("codeBlocks:",codeBlocks)
|
|
|
|
|
|
|
|
|
|
|
|
// 存储所有生成的codeDiff
|
|
|
|
// 存储所有生成的codeDiff
|
|
|
|
const allCodeDiffs = [];
|
|
|
|
const allCodeDiffs = [];
|
|
|
|
@ -184,6 +193,8 @@ export class MessageHandler {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// console.log("allCodeDiffs:",allCodeDiffs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 通知前端流传输完成
|
|
|
|
// 通知前端流传输完成
|
|
|
|
this.provider._postMessage({
|
|
|
|
this.provider._postMessage({
|
|
|
|
@ -219,9 +230,20 @@ export class MessageHandler {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error: any) {
|
|
|
|
} catch (error: any) {
|
|
|
|
|
|
|
|
console.log("error:",error)
|
|
|
|
// 检查是否是由于取消请求导致的中断
|
|
|
|
// 检查是否是由于取消请求导致的中断
|
|
|
|
if (error.name === 'AbortError' || error.message.includes('cancel') || error.message.includes('中断')) {
|
|
|
|
if (error.name === 'AbortError' || error.message.includes('cancel') || error.message.includes('中断')) {
|
|
|
|
this.provider._postMessage({command: 'requestCancelled'});
|
|
|
|
this.provider._postMessage({command: 'requestCancelled'});
|
|
|
|
|
|
|
|
} else if (error.message.includes('terminated') || error.message.includes('other side closed') || error.name === 'TypeError') {
|
|
|
|
|
|
|
|
// 处理网络连接中断错误,但仍显示已接收的内容
|
|
|
|
|
|
|
|
this.provider._postMessage({command: 'addMessage', role: 'ai', content: '网络连接中断,但以下为已接收的内容:'});
|
|
|
|
|
|
|
|
// 如果有已接收的内容,也显示出来
|
|
|
|
|
|
|
|
// 通知前端流传输完成
|
|
|
|
|
|
|
|
this.provider._postMessage({
|
|
|
|
|
|
|
|
command: 'endStream',
|
|
|
|
|
|
|
|
content: '',
|
|
|
|
|
|
|
|
codeDiffs: []
|
|
|
|
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this.provider._postMessage({command: 'addMessage', role: 'ai', content: '调用失败:' + error.message});
|
|
|
|
this.provider._postMessage({command: 'addMessage', role: 'ai', content: '调用失败:' + error.message});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|