Skip to content

Commit 4eaa01a

Browse files
committed
feat: 新增,聊天存盘功能
1 parent 572bf5d commit 4eaa01a

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

prompt/chat.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@
1717
��chatpanel ����ļ�¼�洢��һ��markdown�ĵ���
1818
��ʽ���Բ���֮ǰ�����ƴ���
1919
�ļ����� ʱ���_chat_��һ������ĸ���.md
20-
ʱ����ǵ�һ��request����ʱȷ����
20+
ʱ����ǵ�һ��request����ʱȷ����
21+
22+
���������Ҫʵʱ�������¼���̳�markdown
23+
�ο��Ҵ�������markdown���������򣨴��.chat��,
24+
��һ�η����û������ʱ���½�һ���ļ�������������������ļ���
25+
����ÿ��10�뿴һ����û�������ݣ��о͸��Ǵ棨��һ��������ʱ����У�
26+
new session��ʱ��Ҫ���ã����Ǹ��ļ�����գ���ֹ�µĶԻ��浽�ϵ��ļ���
27+
�����markdown���@user �� @AI ����ʾ˫���ĶԻ�
28+
Ҫ������չ�����ԣ�֮����ܻ���������ǰ�������ļ��������ŶԻ�����β���

src/chatPanel.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as vscode from 'vscode';
22
import { callDeepSeekApi } from './deepseekApi';
33
import { getCurrentOperationController, resetCurrentOperationController } from './extension';
4+
import path from 'path';
5+
import * as fs from "fs";
46

57
// Webview 输出通道实现
68
class WebviewOutputChannel implements vscode.OutputChannel {
@@ -53,6 +55,8 @@ export class ChatPanel {
5355
private disposables: vscode.Disposable[] = [];
5456
private conversation: { role: string; content: string }[] = [];
5557
private userMessageIndex: number = 0;
58+
private chatFilePath: string | null = null;
59+
private lastSaveTime: number = Date.now();
5660

5761
private constructor(panel: vscode.WebviewPanel) {
5862
this.panel = panel;
@@ -399,9 +403,14 @@ export class ChatPanel {
399403

400404
if (message.command === 'sendMessage' || message.command === 'editMessage') {
401405
await this.handleSendOrEdit(message, webviewOutputChannel);
406+
this.saveChatToFile();
402407
return;
403408
}
404409

410+
if (message.command === 'newSession') {
411+
this.chatFilePath = null;
412+
}
413+
405414
if (message.command === 'newSession') {
406415
this.conversation = [];
407416
this.userMessageIndex = 0;
@@ -423,6 +432,17 @@ export class ChatPanel {
423432
this.userMessageIndex = message.index;
424433
}
425434

435+
if (!this.chatFilePath) {
436+
const workspaceFolders = vscode.workspace.workspaceFolders;
437+
if (workspaceFolders) {
438+
const workspacePath = workspaceFolders[0].uri.fsPath;
439+
const tmpDir = path.join(workspacePath, '.CodeReDesignWorkSpace');
440+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
441+
const filename = `${timestamp}_chat.chat`;
442+
this.chatFilePath = path.join(tmpDir, filename);
443+
}
444+
}
445+
426446
this.conversation.push({ role: 'user', content: message.text });
427447
this.panel.webview.postMessage({ role: 'user', content: message.text, index: this.userMessageIndex++ });
428448

@@ -458,4 +478,18 @@ export class ChatPanel {
458478
this.disposables.pop()?.dispose();
459479
}
460480
}
481+
482+
private saveChatToFile(): void {
483+
if (!this.chatFilePath) return;
484+
485+
const now = Date.now();
486+
if (now - this.lastSaveTime < 10000) return;
487+
488+
const mdContent = this.conversation.map(msg => {
489+
return `@${msg.role === 'user' ? 'user' : 'AI'}:\n\n${msg.content}\n\n`;
490+
}).join('\n');
491+
492+
fs.writeFileSync(this.chatFilePath, mdContent, 'utf-8');
493+
this.lastSaveTime = now;
494+
}
461495
}

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export async function doUploadCommand(cvbFilePath: string, userPrompt: string, o
101101

102102
// 定义修饰函数,加上表情符号
103103
const decorateWithEmojis = (role : string) => {
104-
return role === "user" ? "🙋‍♂️ **人类**" : "🧠 **AI**";
104+
return role === "user" ? "🙋‍♂️ **User**" : "🧠 **AI**";
105105
};
106106

107107
// 生成Markdown内容,使用修饰函数

src/siderBar.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function registerCvbContextMenu(context: vscode.ExtensionContext) {
4747

4848
// 创建文件系统监听器
4949
const watcher = vscode.workspace.createFileSystemWatcher(
50-
new vscode.RelativePattern(targetFolder, '**/*.{cvb,md}') // 监听子文件夹中的所有 .cvb 文件
50+
new vscode.RelativePattern(targetFolder, '**/*.{cvb,md,chat}') // 监听子文件夹中的所有 .cvb 文件
5151
);
5252

5353
// 当文件变化时刷新视图
@@ -106,6 +106,9 @@ class CvbViewProvider implements vscode.TreeDataProvider<vscode.TreeItem> {
106106
} else if (file.endsWith('.md')) {
107107
files.push(new MDFile(file, uri));
108108
}
109+
else if (file.endsWith('.chat')) {
110+
files.push(new ChatFile(file, uri));
111+
}
109112
});
110113
}
111114

@@ -157,6 +160,22 @@ class MDFile extends vscode.TreeItem {
157160
}
158161
}
159162

163+
class ChatFile extends vscode.TreeItem {
164+
constructor(
165+
public readonly label: string,
166+
public readonly uri: vscode.Uri
167+
) {
168+
super(label, vscode.TreeItemCollapsibleState.None);
169+
this.command = {
170+
command: 'codeReDesign.showFile', // 复用同一个打开命令
171+
title: 'Open Chat File',
172+
arguments: [uri]
173+
};
174+
this.iconPath = new vscode.ThemeIcon('comment-discussion'); // 使用文档图标
175+
this.resourceUri = uri;
176+
this.contextValue = 'chatFile'; // 新的上下文值
177+
}
178+
}
160179

161180
/**
162181
* 处理 .cvb 文件的函数

0 commit comments

Comments
 (0)