feat(extension): 添加创建新文件功能并重构代码
- 在 extension.ts 中添加了创建新文件的逻辑 - 在 webview 中增加了创建文件按钮和相关事件处理 - 重构了 callQwenAPI 和 generateCodeDiff 函数,移至新的 utils 文件夹 - 新增 getFileExtension 函数用于获取文件扩展名master
parent
196a5eba2a
commit
2b51c4cbde
@ -0,0 +1,83 @@
|
||||
import * as diff from "diff";
|
||||
|
||||
export function generateCodeDiff(originalCode: string, aiResponse: string): { added: string[]; removed: string[]; modifiedCode: string } | null {
|
||||
// 提取代码块中的代码
|
||||
const codeBlockRegex = /```(?:([a-zA-Z0-9]+))?\s*[\n\r]([\s\S]*?)\s*```/g;
|
||||
const matches = [...aiResponse.matchAll(codeBlockRegex)];
|
||||
|
||||
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);
|
||||
|
||||
// 如果标准化后的内容相同,则没有差异
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
||||
// 返回差异结果,无论是否有差异
|
||||
return {
|
||||
added,
|
||||
removed,
|
||||
modifiedCode: normalizedModified
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getFileExtension(language: string): string {
|
||||
const languageMap: { [key: string]: string } = {
|
||||
'javascript': '.js',
|
||||
'typescript': '.ts',
|
||||
'python': '.py',
|
||||
'java': '.java',
|
||||
'html': '.html',
|
||||
'css': '.css',
|
||||
'json': '.json',
|
||||
'c': '.c',
|
||||
'cpp': '.cpp',
|
||||
'csharp': '.cs',
|
||||
'php': '.php',
|
||||
'ruby': '.rb',
|
||||
'go': '.go',
|
||||
'rust': '.rs',
|
||||
'swift': '.swift',
|
||||
'kotlin': '.kt',
|
||||
'scala': '.scala',
|
||||
'dart': '.dart',
|
||||
'lua': '.lua',
|
||||
'perl': '.pl',
|
||||
'r': '.r',
|
||||
'sql': '.sql',
|
||||
'yaml': '.yaml',
|
||||
'xml': '.xml'
|
||||
};
|
||||
|
||||
return languageMap[language?.toLowerCase()] || '.txt';
|
||||
}
|
||||
|
||||
@ -0,0 +1,52 @@
|
||||
import vscode from "vscode";
|
||||
|
||||
export async function callQwenAPI(
|
||||
question: string,
|
||||
history: any[],
|
||||
fileContent: string,
|
||||
context: vscode.ExtensionContext
|
||||
): Promise<any> {
|
||||
const apiKey = 'dev-token';
|
||||
const url = 'https://aicomp.ngsk.tech:7001/api/v1/chat/completions';
|
||||
|
||||
const messages = [
|
||||
{
|
||||
role: 'system',
|
||||
content: `你是一个代码助手,请根据当前文件内容和历史对话回答问题。请使用中文回答。`
|
||||
},
|
||||
...history,
|
||||
{
|
||||
role: 'user',
|
||||
content: `【当前文件内容】:
|
||||
\`\`\`${fileContent}\`\`\`
|
||||
|
||||
【用户提问】:
|
||||
${question}`
|
||||
}
|
||||
];
|
||||
|
||||
console.log("messages:",messages)
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: 'qwen2.5-local',
|
||||
messages
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('API 请求失败');
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error: any) {
|
||||
console.error(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue