Skip to content

Commit 3cfa071

Browse files
committed
fix:优化聊天存盘机制
1 parent b9dd7ac commit 3cfa071

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

src/chatPanel.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import { getOutputChannel } from './extension';
1010
class WebviewOutputChannel implements vscode.OutputChannel {
1111
private readonly webview: vscode.Webview;
1212
private readonly channelName: string;
13+
private readonly onChanged?: () => void;
1314

14-
constructor(webview: vscode.Webview, name: string) {
15+
constructor(webview: vscode.Webview, name: string, onChanged?: () => void ) {
1516
this.webview = webview;
1617
this.channelName = name;
18+
this.onChanged = onChanged;
1719
}
1820

1921
get name(): string {
@@ -23,11 +25,19 @@ class WebviewOutputChannel implements vscode.OutputChannel {
2325
append(value: string): void {
2426
this.webview.postMessage({ role: 'model', content: value });
2527
getOutputChannel().append(value);
28+
29+
if (this.onChanged) {
30+
this.onChanged();
31+
}
2632
}
2733

2834
appendLine(value: string): void {
2935
this.append(value + '\n');
3036
getOutputChannel().append(value + '\n');
37+
38+
if (this.onChanged) {
39+
this.onChanged();
40+
}
3141
}
3242

3343
clear(): void {
@@ -52,9 +62,10 @@ export class ChatPanel {
5262
private conversation: { role: string; content: string }[] = [];
5363
private userMessageIndex: number = 0;
5464
private chatFilePath: string | null = null;
55-
private lastSaveTime: number = Date.now();
5665
private readonly context: vscode.ExtensionContext;
5766
private isFilenameCustomized: boolean = false;
67+
private isSaveScheduled = false;
68+
private saveTimeout: NodeJS.Timeout | null = null;
5869

5970
private constructor(panel: vscode.WebviewPanel, context: vscode.ExtensionContext) {
6071
this.panel = panel;
@@ -63,6 +74,20 @@ export class ChatPanel {
6374
this.panel.webview.html = this.getHtmlForWebview();
6475
}
6576

77+
// 延迟保存方法
78+
private delaySave() {
79+
// 如果已经有保存任务排队,跳过新请求
80+
if (this.isSaveScheduled) {return; }
81+
82+
this.isSaveScheduled = true;
83+
84+
// 设置2秒延迟保存
85+
this.saveTimeout = setTimeout(() => {
86+
this.saveChatToFile(); // 实际保存操作
87+
this.isSaveScheduled = false; // 重置标志位
88+
}, 2000);
89+
}
90+
6691
public static createOrShow(context: vscode.ExtensionContext): void {
6792
const column = vscode.window.activeTextEditor?.viewColumn;
6893

@@ -195,13 +220,13 @@ export class ChatPanel {
195220
}
196221

197222
private async handleMessage(message: any): Promise<void> {
198-
const webviewOutputChannel = new WebviewOutputChannel(this.panel.webview, 'DeepSeek API Output');
223+
const webviewOutputChannel = new WebviewOutputChannel(this.panel.webview, 'DeepSeek API Output', this.delaySave);
199224

200225
switch (message.command) {
201226
case 'sendMessage':
202227
case 'editMessage':
203228
await this.handleSendOrEditMessage(message, webviewOutputChannel);
204-
this.saveChatToFile();
229+
this.delaySave();
205230
break;
206231

207232
case 'newSession':
@@ -269,7 +294,7 @@ export class ChatPanel {
269294

270295
try {
271296
// 截取前30个字符
272-
const requestSnippet = userMessage.slice(0, 30).replace(/[\n\r]/g, ' ');
297+
const requestSnippet = userMessage.slice(0, 32).replace(/[\n\r]/g, ' ');
273298
const summary = await generateFilenameFromRequest(requestSnippet);
274299

275300
// 生成新路径
@@ -364,10 +389,9 @@ export class ChatPanel {
364389
}
365390

366391
private saveChatToFile(): void {
367-
if (!this.chatFilePath || Date.now() - this.lastSaveTime < 10000) {
392+
if (!this.chatFilePath) {
368393
return;
369394
}
370-
371395
try {
372396
const mdContent = this.conversation.map(msg => {
373397
return `@${msg.role === 'user' ? 'user' : 'AI'}:\n\n${msg.content}\n\n`;
@@ -380,7 +404,6 @@ export class ChatPanel {
380404
}
381405

382406
fs.writeFileSync(this.chatFilePath, mdContent, 'utf-8');
383-
this.lastSaveTime = Date.now();
384407
} catch (error) {
385408
console.error('Failed to save chat file:', error);
386409
}

0 commit comments

Comments
 (0)