11import * as vscode from 'vscode' ;
22import { callDeepSeekApi } from './deepseekApi' ;
33import { getCurrentOperationController , resetCurrentOperationController } from './extension' ;
4+ import path from 'path' ;
5+ import * as fs from "fs" ;
46
57// Webview 输出通道实现
68class 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}
0 commit comments