|
1 | 1 | import * as vscode from 'vscode'; |
2 | | -import { callDeepSeekApi } from './deepseekApi'; |
| 2 | +import { callDeepSeekApi, generateFilenameFromRequest } from './deepseekApi'; |
3 | 3 | import { getCurrentOperationController, resetCurrentOperationController } from './extension'; |
4 | 4 | import path from 'path'; |
5 | 5 | import * as fs from "fs"; |
@@ -54,6 +54,7 @@ export class ChatPanel { |
54 | 54 | private chatFilePath: string | null = null; |
55 | 55 | private lastSaveTime: number = Date.now(); |
56 | 56 | private readonly context: vscode.ExtensionContext; |
| 57 | + private isFilenameCustomized: boolean = false; |
57 | 58 |
|
58 | 59 | private constructor(panel: vscode.WebviewPanel, context: vscode.ExtensionContext) { |
59 | 60 | this.panel = panel; |
@@ -221,6 +222,10 @@ export class ChatPanel { |
221 | 222 | } |
222 | 223 |
|
223 | 224 | this.prepareChatFilePath(); |
| 225 | + |
| 226 | + if (this.conversation.length === 0) { // 只有第一次消息时处理 |
| 227 | + await this.tryGenerateCustomFilename(message.text); |
| 228 | + } |
224 | 229 |
|
225 | 230 | this.conversation.push({ role: 'user', content: message.text }); |
226 | 231 | this.panel.webview.postMessage({ |
@@ -254,6 +259,38 @@ export class ChatPanel { |
254 | 259 | } |
255 | 260 | } |
256 | 261 |
|
| 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 | + |
257 | 294 | private updateUIForSending(): void { |
258 | 295 | this.panel.webview.postMessage({ command: 'disableSendButton' }); |
259 | 296 | this.panel.webview.postMessage({ command: 'showStopButton' }); |
@@ -311,6 +348,7 @@ export class ChatPanel { |
311 | 348 |
|
312 | 349 | private handleNewSession(): void { |
313 | 350 | this.chatFilePath = null; |
| 351 | + this.isFilenameCustomized = false; |
314 | 352 | this.conversation = []; |
315 | 353 | this.userMessageIndex = 0; |
316 | 354 | resetCurrentOperationController(); |
@@ -354,6 +392,7 @@ export class ChatPanel { |
354 | 392 |
|
355 | 393 | this.createOrShow(context); |
356 | 394 |
|
| 395 | + ChatPanel.currentPanel!.isFilenameCustomized = true; |
357 | 396 | ChatPanel.currentPanel!.chatFilePath = filePath; |
358 | 397 | ChatPanel.currentPanel!.conversation = conversation; |
359 | 398 | ChatPanel.currentPanel!.userMessageIndex = conversation.filter(m => m.role === 'user').length; |
|
0 commit comments