Skip to content

Commit d3e6ba8

Browse files
committed
feat:聊天记录自动根据初始用户需求关键字命名
1 parent 6cd946d commit d3e6ba8

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/chatPanel.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode';
2-
import { callDeepSeekApi } from './deepseekApi';
2+
import { callDeepSeekApi, generateFilenameFromRequest } from './deepseekApi';
33
import { getCurrentOperationController, resetCurrentOperationController } from './extension';
44
import path from 'path';
55
import * as fs from "fs";
@@ -54,6 +54,7 @@ export class ChatPanel {
5454
private chatFilePath: string | null = null;
5555
private lastSaveTime: number = Date.now();
5656
private readonly context: vscode.ExtensionContext;
57+
private isFilenameCustomized: boolean = false;
5758

5859
private constructor(panel: vscode.WebviewPanel, context: vscode.ExtensionContext) {
5960
this.panel = panel;
@@ -221,6 +222,10 @@ export class ChatPanel {
221222
}
222223

223224
this.prepareChatFilePath();
225+
226+
if (this.conversation.length === 0) { // 只有第一次消息时处理
227+
await this.tryGenerateCustomFilename(message.text);
228+
}
224229

225230
this.conversation.push({ role: 'user', content: message.text });
226231
this.panel.webview.postMessage({
@@ -254,6 +259,38 @@ export class ChatPanel {
254259
}
255260
}
256261

262+
private async tryGenerateCustomFilename(userMessage: string): Promise<void> {
263+
if (this.isFilenameCustomized) { return; }
264+
265+
const originalPath = this.chatFilePath;
266+
if (!originalPath || !originalPath.endsWith('_chat.chat')) {
267+
return;
268+
}
269+
270+
try {
271+
// 截取前30个字符
272+
const requestSnippet = userMessage.slice(0, 30).replace(/[\n\r]/g, ' ');
273+
const summary = await generateFilenameFromRequest(requestSnippet);
274+
275+
// 生成新路径
276+
const newFilename = `${path.basename(originalPath, '_chat.chat')}_${summary}.chat`;
277+
const newPath = path.join(path.dirname(originalPath), newFilename);
278+
279+
// 更新状态
280+
this.chatFilePath = newPath;
281+
this.isFilenameCustomized = true;
282+
283+
// 重命名文件
284+
if (fs.existsSync(originalPath)) {
285+
await fs.promises.rename(originalPath, newPath);
286+
}
287+
} catch (error) {
288+
console.error('Failed to rename chat file:', error);
289+
// 失败时保持原文件名
290+
this.chatFilePath = originalPath;
291+
}
292+
}
293+
257294
private updateUIForSending(): void {
258295
this.panel.webview.postMessage({ command: 'disableSendButton' });
259296
this.panel.webview.postMessage({ command: 'showStopButton' });
@@ -311,6 +348,7 @@ export class ChatPanel {
311348

312349
private handleNewSession(): void {
313350
this.chatFilePath = null;
351+
this.isFilenameCustomized = false;
314352
this.conversation = [];
315353
this.userMessageIndex = 0;
316354
resetCurrentOperationController();
@@ -354,6 +392,7 @@ export class ChatPanel {
354392

355393
this.createOrShow(context);
356394

395+
ChatPanel.currentPanel!.isFilenameCustomized = true;
357396
ChatPanel.currentPanel!.chatFilePath = filePath;
358397
ChatPanel.currentPanel!.conversation = conversation;
359398
ChatPanel.currentPanel!.userMessageIndex = conversation.filter(m => m.role === 'user').length;

0 commit comments

Comments
 (0)