|
|
|
|
@ -64,7 +64,7 @@ export class MessageHandler {
|
|
|
|
|
this.handleRestoreState(message);
|
|
|
|
|
break;
|
|
|
|
|
case 'log' :
|
|
|
|
|
console.log( ...message.data);
|
|
|
|
|
console.log(...message.data);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -99,14 +99,46 @@ export class MessageHandler {
|
|
|
|
|
if (done) break;
|
|
|
|
|
|
|
|
|
|
const chunk = decoder.decode(value, {stream: true});
|
|
|
|
|
console.log("chunk:",chunk)
|
|
|
|
|
console.log("chunk:", chunk)
|
|
|
|
|
|
|
|
|
|
// 检查是否包含 event: explanation
|
|
|
|
|
if (chunk.includes('event: explanation')) {
|
|
|
|
|
let explanationChunk = chunk;
|
|
|
|
|
// 直接提取 data: 后面的所有内容
|
|
|
|
|
const explanationIndex = chunk.indexOf('data: ');
|
|
|
|
|
explanationChunk = chunk.substring(explanationIndex, explanationChunk.length - 1);
|
|
|
|
|
|
|
|
|
|
// 处理截取后的内容
|
|
|
|
|
if (explanationChunk.startsWith('data:')) {
|
|
|
|
|
// 移除data:前缀
|
|
|
|
|
explanationChunk = explanationChunk.replace(/^data:/, '');
|
|
|
|
|
// 逐个匹配换行符,当出现两个或更多连续换行符时删除多余的
|
|
|
|
|
explanationChunk = explanationChunk.replace(/^(\n{2,})/, (match) => {
|
|
|
|
|
// 删除所有连续的换行符
|
|
|
|
|
return '';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (explanationChunk) {
|
|
|
|
|
let explanationText = explanationChunk;
|
|
|
|
|
console.log("explanationText:", explanationText);
|
|
|
|
|
|
|
|
|
|
// 发送 explanation 到前端
|
|
|
|
|
this.provider._postMessage({
|
|
|
|
|
command: 'explanation',
|
|
|
|
|
data: explanationText
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
continue; // 跳过这个 chunk 的其他处理
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查是否包含event: end事件
|
|
|
|
|
let processedChunk = chunk;
|
|
|
|
|
if (chunk.includes('event: end')) {
|
|
|
|
|
// 只处理event: end之前的内容
|
|
|
|
|
const endIndex = chunk.indexOf('event: end');
|
|
|
|
|
processedChunk = chunk.substring(0, endIndex);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 处理截取后的内容
|
|
|
|
|
if (processedChunk.startsWith('data:')) {
|
|
|
|
|
// 移除data:前缀
|
|
|
|
|
@ -117,21 +149,21 @@ export class MessageHandler {
|
|
|
|
|
return '';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (processedChunk) {
|
|
|
|
|
aiMessage += processedChunk;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 发送流式数据到前端
|
|
|
|
|
this.provider._postMessage({
|
|
|
|
|
command: 'streamData',
|
|
|
|
|
data: processedChunk
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 停止继续处理
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (chunk.startsWith('data:')) {
|
|
|
|
|
// 移除data:前缀
|
|
|
|
|
processedChunk = chunk.replace(/^data:/, '');
|
|
|
|
|
@ -180,14 +212,14 @@ export class MessageHandler {
|
|
|
|
|
// 解析AI响应中的多个代码块并生成独立的codeDiff信息
|
|
|
|
|
const codeBlocks = aiMessage.match(/```[\s\S]*?```/g) || [];
|
|
|
|
|
// console.log("codeBlocks:",codeBlocks)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 存储所有生成的codeDiff
|
|
|
|
|
const allCodeDiffs = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const block of codeBlocks) {
|
|
|
|
|
// 生成codeDiff
|
|
|
|
|
const codeDiff = generateCodeDiff(fileContent, block);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (codeDiff) {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
allCodeDiffs.push(codeDiff);
|
|
|
|
|
@ -210,10 +242,10 @@ export class MessageHandler {
|
|
|
|
|
for (let i = 0; i < allCodeDiffs.length; i++) {
|
|
|
|
|
const codeDiff = allCodeDiffs[i];
|
|
|
|
|
const filePath = message.fileContentPath;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建唯一的工作区变更键
|
|
|
|
|
const changeKey = `${filePath}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
workspaceChanges[changeKey] = {
|
|
|
|
|
original: fileContent,
|
|
|
|
|
@ -231,13 +263,17 @@ export class MessageHandler {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
console.log("error:",error)
|
|
|
|
|
console.log("error:", error)
|
|
|
|
|
// 检查是否是由于取消请求导致的中断
|
|
|
|
|
if (error.name === 'AbortError' || error.message.includes('cancel') || error.message.includes('中断')) {
|
|
|
|
|
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: 'addMessage',
|
|
|
|
|
role: 'ai',
|
|
|
|
|
content: '网络连接中断,但以下为已接收的内容:'
|
|
|
|
|
});
|
|
|
|
|
// 如果有已接收的内容,也显示出来
|
|
|
|
|
// 通知前端流传输完成
|
|
|
|
|
this.provider._postMessage({
|
|
|
|
|
|