Skip to content

Commit 5949703

Browse files
committed
feat:新增聊天面板
1 parent c4758e8 commit 5949703

File tree

5 files changed

+136
-4
lines changed

5 files changed

+136
-4
lines changed

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
"category": "CodeReDesign",
5858
"icon": "${bug}"
5959
},
60+
{
61+
"command": "codeReDesign.startChat",
62+
"title": "startChat",
63+
"category": "CodeReDesign",
64+
"icon": "${chat}"
65+
},
6066
{
6167
"command": "codeReDesign.applyThisCvb",
6268
"title": "sidebar Apply this CVB to Workspace",
@@ -295,4 +301,4 @@
295301
"jschardet": "^3.1.4",
296302
"openai": "^4.81.0"
297303
}
298-
}
304+
}

src/chatPanel.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import * as vscode from 'vscode';
2+
import { callDeepSeekApi } from './deepseekApi';
3+
4+
export class ChatPanel {
5+
private static readonly viewType = 'chatPanel';
6+
private static currentPanel: ChatPanel | undefined;
7+
private readonly _panel: vscode.WebviewPanel;
8+
private _disposables: vscode.Disposable[] = [];
9+
private _conversation: { role: string, content: string }[] = [];
10+
11+
private constructor(panel: vscode.WebviewPanel) {
12+
this._panel = panel;
13+
this._panel.onDidDispose(() => this.dispose(), null, this._disposables);
14+
this._panel.webview.html = this._getHtmlForWebview();
15+
this._panel.webview.onDidReceiveMessage(this._handleMessage, this, this._disposables);
16+
}
17+
18+
public static createOrShow() {
19+
const column = vscode.window.activeTextEditor
20+
? vscode.window.activeTextEditor.viewColumn
21+
: undefined;
22+
23+
if (ChatPanel.currentPanel) {
24+
ChatPanel.currentPanel._panel.reveal(column);
25+
return;
26+
}
27+
28+
const panel = vscode.window.createWebviewPanel(
29+
ChatPanel.viewType,
30+
'Chat with Model',
31+
column || vscode.ViewColumn.One,
32+
{
33+
enableScripts: true,
34+
retainContextWhenHidden: true,
35+
}
36+
);
37+
38+
ChatPanel.currentPanel = new ChatPanel(panel);
39+
}
40+
41+
private _getHtmlForWebview() {
42+
return `
43+
<!DOCTYPE html>
44+
<html lang="en">
45+
<head>
46+
<meta charset="UTF-8">
47+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
48+
<title>Chat with Model</title>
49+
<style>
50+
.user { color: blue; }
51+
.model { color: green; }
52+
</style>
53+
</head>
54+
<body>
55+
<div id="chat"></div>
56+
<input type="text" id="input" placeholder="Type your message here..." />
57+
<button id="reset">Reset</button>
58+
<script>
59+
const vscode = acquireVsCodeApi();
60+
const chat = document.getElementById('chat');
61+
const input = document.getElementById('input');
62+
const reset = document.getElementById('reset');
63+
64+
input.addEventListener('keydown', (event) => {
65+
if (event.key === 'Enter') {
66+
vscode.postMessage({
67+
command: 'sendMessage',
68+
text: input.value
69+
});
70+
input.value = '';
71+
}
72+
});
73+
74+
reset.addEventListener('click', () => {
75+
vscode.postMessage({
76+
command: 'reset'
77+
});
78+
});
79+
80+
window.addEventListener('message', (event) => {
81+
const { role, content } = event.data;
82+
const div = document.createElement('div');
83+
div.className = role;
84+
div.textContent = content;
85+
chat.appendChild(div);
86+
});
87+
</script>
88+
</body>
89+
</html>
90+
`;
91+
}
92+
93+
private async _handleMessage(message: any) {
94+
switch (message.command) {
95+
case 'sendMessage':
96+
this._conversation.push({ role: 'user', content: message.text });
97+
this._panel.webview.postMessage({ role: 'user', content: message.text });
98+
const response = await callDeepSeekApi(message.text, 'You are a helpful assistant.');
99+
this._conversation.push({ role: 'model', content: response || '' });
100+
this._panel.webview.postMessage({ role: 'model', content: response || '' });
101+
break;
102+
case 'reset':
103+
this._conversation = [];
104+
this._panel.webview.html = this._getHtmlForWebview();
105+
break;
106+
}
107+
}
108+
109+
public dispose() {
110+
ChatPanel.currentPanel = undefined;
111+
this._panel.dispose();
112+
while (this._disposables.length) {
113+
const disposable = this._disposables.pop();
114+
if (disposable) {
115+
disposable.dispose();
116+
}
117+
}
118+
}
119+
}

src/deepseekApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function GetLastMessageBody() : OpenAI.ChatCompletionMessageParam[] {
5555
* @param abortSignal 用于中断请求的信号
5656
* @returns API 返回的完整内容
5757
*/
58-
async function callDeepSeekApi(
58+
export async function callDeepSeekApi(
5959
userContent: string,
6060
systemContent: string = 'You are a helpful assistant.',
6161
outputChannel?: vscode.OutputChannel,

src/extension.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { setupCvbAsMarkdown } from './cvbMarkdownHandler';
88
import { registerCvbContextMenu } from './siderBar';
99
import { showInputMultiLineBox } from './UIComponents';
1010
import { activateGuide } from './guide';
11+
import {ChatPanel} from './chatPanel';
1112

1213
let currentOperationController: AbortController | null = null;
1314

@@ -134,6 +135,11 @@ export function activate(context: vscode.ExtensionContext) {
134135
// 创建输出通道
135136
const outputChannel = vscode.window.createOutputChannel('CodeReDesign API Stream', 'markdown');
136137

138+
// 注册命令:开始对话
139+
let startChatCommand = vscode.commands.registerCommand('codeReDesign.startChat', () => {
140+
ChatPanel.createOrShow();
141+
});
142+
137143
// 注册命令:选择文件并生成 CVB
138144
let generateCvbCommand = vscode.commands.registerCommand('codeReDesign.generateCvb', async () => {
139145
const workspaceFolders = vscode.workspace.workspaceFolders;
@@ -317,7 +323,7 @@ export function activate(context: vscode.ExtensionContext) {
317323
}
318324
});
319325

320-
context.subscriptions.push(generateCvbCommand, uploadCvbCommand, applyCvbCommand, stopOperation, analyzeCodeCommand, outputChannel);
326+
context.subscriptions.push(generateCvbCommand, uploadCvbCommand, applyCvbCommand, stopOperation, analyzeCodeCommand, outputChannel, startChatCommand);
321327

322328
setupCvbAsMarkdown(context);
323329

src/languageMapping.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ export const g_objLanguageMapping: { [key: string]: string } = {
2020
'php': 'php', // PHP
2121
'rust': 'rust', // Rust
2222
'dart': 'dart', // Dart
23-
'md': 'markdown' // markdown
23+
'md': 'markdown', // markdown
24+
'json':'json'
2425
};

0 commit comments

Comments
 (0)