|
|
|
|
@ -1,7 +1,10 @@
|
|
|
|
|
import * as vscode from 'vscode';
|
|
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as diff from 'diff';
|
|
|
|
|
|
|
|
|
|
let panel: vscode.WebviewPanel | undefined;
|
|
|
|
|
let currentSessionHistory: { role: string; content: string }[] = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function activate(context: vscode.ExtensionContext) {
|
|
|
|
|
console.log('Extension activated');
|
|
|
|
|
@ -127,6 +130,9 @@ function openWebview(
|
|
|
|
|
globalState: vscode.Memento,
|
|
|
|
|
context: vscode.ExtensionContext // 添加 context 参数
|
|
|
|
|
) {
|
|
|
|
|
// 每次打开新的 Webview 时清空当前会话历史
|
|
|
|
|
currentSessionHistory = [];
|
|
|
|
|
|
|
|
|
|
panel = vscode.window.createWebviewPanel(
|
|
|
|
|
'aiChatWebview',
|
|
|
|
|
'AI Chat',
|
|
|
|
|
@ -166,22 +172,30 @@ function openWebview(
|
|
|
|
|
case 'ask':
|
|
|
|
|
const question = message.text;
|
|
|
|
|
const fileContent = message.fileContent;
|
|
|
|
|
const history = globalState.get<{ role: string; content: string }[]>('chatHistory', []);
|
|
|
|
|
const history = currentSessionHistory;
|
|
|
|
|
|
|
|
|
|
const newHistory = [...history, { role: 'user', content: question }];
|
|
|
|
|
globalState.update('chatHistory', newHistory);
|
|
|
|
|
currentSessionHistory = [...history, { role: 'user', content: question }];
|
|
|
|
|
|
|
|
|
|
panel.webview.postMessage({ command: 'addMessage', role: 'user', content: question });
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await callQwenAPI(question, history, fileContent, context);
|
|
|
|
|
console.log("response:",response)
|
|
|
|
|
console.log("response:", response);
|
|
|
|
|
const aiMessage = response.choices[0]?.message?.content || '未获取到回复';
|
|
|
|
|
|
|
|
|
|
const updatedHistory = [...newHistory, { role: 'assistant', content: aiMessage }];
|
|
|
|
|
globalState.update('chatHistory', updatedHistory);
|
|
|
|
|
// 检查是否包含代码修改
|
|
|
|
|
const codeDiff = generateCodeDiff(fileContent, aiMessage);
|
|
|
|
|
console.log("codeDiff:",codeDiff)
|
|
|
|
|
|
|
|
|
|
panel.webview.postMessage({ command: 'addMessage', role: 'ai', content: aiMessage });
|
|
|
|
|
// 更新当前会话历史
|
|
|
|
|
currentSessionHistory = [...currentSessionHistory, { role: 'assistant', content: aiMessage }];
|
|
|
|
|
|
|
|
|
|
panel.webview.postMessage({
|
|
|
|
|
command: 'addMessage',
|
|
|
|
|
role: 'ai',
|
|
|
|
|
content: aiMessage,
|
|
|
|
|
codeDiff: codeDiff // 发送代码差异信息
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
panel.webview.postMessage({ command: 'addMessage', role: 'ai', content: '调用失败:' + error.message });
|
|
|
|
|
}
|
|
|
|
|
@ -253,6 +267,13 @@ function openWebview(
|
|
|
|
|
undefined,
|
|
|
|
|
context.subscriptions
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 添加 dispose 监听器
|
|
|
|
|
panel.onDidDispose(() => {
|
|
|
|
|
// 面板关闭时清空当前会话历史
|
|
|
|
|
currentSessionHistory = [];
|
|
|
|
|
panel = undefined;
|
|
|
|
|
}, null, context.subscriptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function callQwenAPI(
|
|
|
|
|
@ -269,6 +290,7 @@ async function callQwenAPI(
|
|
|
|
|
role: 'system',
|
|
|
|
|
content: `你是一个代码助手,请根据当前文件内容和历史对话回答问题。请使用中文回答。`
|
|
|
|
|
},
|
|
|
|
|
...history,
|
|
|
|
|
{
|
|
|
|
|
role: 'user',
|
|
|
|
|
content: `【当前文件内容】:
|
|
|
|
|
@ -305,6 +327,58 @@ async function callQwenAPI(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function generateCodeDiff(originalCode: string, aiResponse: string): { added: string[]; removed: string[] } | null {
|
|
|
|
|
console.log("模型返回值:",aiResponse)
|
|
|
|
|
// 提取代码块中的代码
|
|
|
|
|
const codeBlockRegex = /```(?:([a-zA-Z0-9]+))?\s*[\n\r]([\s\S]*?)\s*```/g;
|
|
|
|
|
const matches = [...aiResponse.matchAll(codeBlockRegex)];
|
|
|
|
|
console.log("正则过滤:",matches)
|
|
|
|
|
|
|
|
|
|
if (matches.length > 0) {
|
|
|
|
|
const modifiedCode = matches[0][2]; // 提取第一个代码块中的代码
|
|
|
|
|
|
|
|
|
|
// 智能标准化函数
|
|
|
|
|
const smartNormalize = (code: string): string => {
|
|
|
|
|
return code
|
|
|
|
|
.split('\n')
|
|
|
|
|
.map(line => line.trimEnd()) // 使用 trimEnd() 移除行尾空白
|
|
|
|
|
.filter(line => line !== null && line !== undefined) // 过滤空行
|
|
|
|
|
.join('\n')
|
|
|
|
|
.trim(); // 整体去除首尾空白
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const normalizedOriginal = smartNormalize(originalCode);
|
|
|
|
|
const normalizedModified = smartNormalize(modifiedCode);
|
|
|
|
|
console.log("原始内容:",normalizedOriginal)
|
|
|
|
|
console.log("模型内容:",normalizedModified)
|
|
|
|
|
|
|
|
|
|
// 如果标准化后的内容相同,则没有差异
|
|
|
|
|
if (normalizedOriginal === normalizedModified) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
// 使用更精确的 diff 算法
|
|
|
|
|
const diffResult = diff.diffLines(normalizedOriginal, normalizedModified, { ignoreWhitespace: true });
|
|
|
|
|
|
|
|
|
|
const added: string[] = [];
|
|
|
|
|
const removed: string[] = [];
|
|
|
|
|
|
|
|
|
|
diffResult.forEach(part => {
|
|
|
|
|
if (part.added) {
|
|
|
|
|
added.push(part.value);
|
|
|
|
|
} else if (part.removed) {
|
|
|
|
|
removed.push(part.value);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 只有当确实有差异时才返回
|
|
|
|
|
if (added.length > 0 || removed.length > 0) {
|
|
|
|
|
return { added, removed };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getWebviewContent(styleUri: vscode.Uri, scriptUri: vscode.Uri,highlightScriptUri:vscode.Uri,highlightStyleUri:vscode.Uri,markedScriptUri:vscode.Uri): string {
|
|
|
|
|
return `
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
@ -353,6 +427,19 @@ function getWebviewContent(styleUri: vscode.Uri, scriptUri: vscode.Uri,highlight
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- 代码差异模态框 -->
|
|
|
|
|
<div id="diff-modal" class="modal hidden">
|
|
|
|
|
<div class="modal-content diff-modal-content">
|
|
|
|
|
<div class="modal-header">
|
|
|
|
|
<h3>代码差异对比</h3>
|
|
|
|
|
<span id="close-diff-modal" class="close-modal">×</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="modal-body">
|
|
|
|
|
<div id="diff-content" class="diff-content"></div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- 聊天输入表单 -->
|
|
|
|
|
<form id="chat-form">
|
|
|
|
|
<input type="text" id="user-input" placeholder="输入你的问题..." required />
|
|
|
|
|
|