Skip to content

Commit 627819d

Browse files
committed
增加MCP:代码理解
1 parent a633dcb commit 627819d

7 files changed

Lines changed: 48 additions & 16 deletions

File tree

README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,12 @@
66

77
## 中文联想支持
88

9-
![alt text](images/效果图1.png)
10-
119
## AI智能注释
1210

13-
![alt text](images/效果图2.png)
14-
1511
## AI智能报错分析
1612

17-
![alt text](images/效果图4.png)
18-
1913
## 两套主题流畅切换
2014

21-
![alt text](images/image.png)
22-
2315
## 通过npm run dev 构建全新的提示词工程
2416

2517
## 特性

iframe/main/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
//注册EDA智能补全
120120
ACE_CodingForEDA(editor, edcode);
121121

122-
//加载自动缓存
122+
//自动缓存
123123
editor.session.on('change', function () {
124124
eda.sys_Storage.setExtensionUserConfig('editor_value', editor.getValue());
125125
});

iframe/script/User_config/Ai_Chat.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,21 +247,61 @@ function initAiChat() {
247247

248248
// --- 核心发送逻辑 ---
249249
async function handleSend() {
250-
const text = inputArea.value.trim();
251-
if (!text) return;
250+
const rawText = inputArea.value.trim();
251+
if (!rawText) return;
252252

253253
if (!chatConfig.apiKey) {
254254
alert('请先配置 API Key');
255255
openModal();
256256
return;
257257
}
258258

259-
appendMessage('user', text);
259+
// --- 新增逻辑:检测“理解代码”类指令并自动获取编辑器内容 ---
260+
let finalUserText = rawText;
261+
const codeKeywords = [
262+
'理解我的代码',
263+
'分析这段代码',
264+
'解释代码',
265+
'看我的代码',
266+
'帮我优化代码',
267+
'代码有什么问题',
268+
'检查代码错误',
269+
'重构这段代码',
270+
];
271+
272+
// 检查是否触发自动获取代码逻辑
273+
const shouldFetchCode = codeKeywords.some((keyword) => rawText.includes(keyword));
274+
275+
if (shouldFetchCode) {
276+
// 检查 editor 对象是否存在且可用 (确保 editor 已在全局或当前作用域初始化)
277+
if (typeof editor !== 'undefined' && editor && typeof editor.getValue === 'function') {
278+
const currentCode = editor.getValue();
279+
280+
if (!currentCode || currentCode.trim() === '') {
281+
// 如果编辑器为空,给予提示
282+
finalUserText = `${rawText}\n\n[注意:当前编辑器内容为空,无法提供代码供你分析。]`;
283+
} else {
284+
// 构造包含代码的完整提示词
285+
// 格式:用户指令 + 分隔符 + 代码块
286+
finalUserText = `${rawText}\n\n以下是当前编辑器中的代码:\n\`\`\`\n${currentCode}\n\`\`\``;
287+
console.log('已自动抓取编辑器代码并附加到请求中');
288+
}
289+
} else {
290+
console.warn('未检测到 editor 对象,无法自动获取代码。请确保 editor 已全局初始化。');
291+
// 如果 editor 不存在,保持原样发送,依靠用户手动粘贴
292+
}
293+
}
294+
// --- 新增逻辑结束 ---
295+
296+
// 界面上显示用户原始输入
297+
appendMessage('user', rawText);
260298
inputArea.value = '';
261299
inputArea.style.height = 'auto';
262300

263301
const loadingElements = appendMessage('system', '', false, true);
264-
const currentMessages = [...messageHistory, { role: 'user', content: text }];
302+
303+
// 发送时使用 finalUserText (可能包含自动注入的代码)
304+
const currentMessages = [...messageHistory, { role: 'user', content: finalUserText }];
265305

266306
let aiBubble = null;
267307
let fullResponse = '';
@@ -281,7 +321,7 @@ function initAiChat() {
281321
body: JSON.stringify({
282322
model: chatConfig.model,
283323
messages: currentMessages,
284-
stream: false,
324+
stream: false, // 注意:原代码此处强制为 false,实际使用模拟流式
285325
temperature: 0.7,
286326
}),
287327
});
@@ -323,7 +363,6 @@ function initAiChat() {
323363
hljs.highlightElement(block);
324364
});
325365
}
326-
// 打字结束后添加复制按钮
327366
addCopyButtons(aiBubble);
328367
}
329368
chatList.scrollTop = chatList.scrollHeight;
@@ -338,7 +377,8 @@ function initAiChat() {
338377

339378
function finishProcess() {
340379
if (chatConfig.multiTurn) {
341-
messageHistory.push({ role: 'user', content: text });
380+
// 存入历史记录时使用 finalUserText 以保持上下文(包含代码)
381+
messageHistory.push({ role: 'user', content: finalUserText });
342382
messageHistory.push({ role: 'assistant', content: fullResponse });
343383
if (messageHistory.length > 20) messageHistory = messageHistory.slice(-20);
344384
}

images/image.png

140 KB
Loading

images/效果图1.png

-19.8 KB
Binary file not shown.

images/效果图2.png

-113 KB
Binary file not shown.

images/效果图4.png

-335 KB
Binary file not shown.

0 commit comments

Comments
 (0)