@@ -97,7 +97,6 @@ import { OutputKind } from '../../tools/toolShared'
9797import { ToolUtils , Tool , ToolType } from '../../tools/toolUtils'
9898import { ChatStream } from '../../tools/chatStream'
9999import { ChatHistoryStorage } from '../../storages/chatHistoryStorage'
100- import { FsWriteParams } from '../../tools/fsWrite'
101100import { tempDirPath } from '../../../shared/filesystemUtilities'
102101import { Database } from '../../../shared/db/chatDb/chatDb'
103102import { TabBarController } from './tabBarController'
@@ -746,13 +745,6 @@ export class ChatController {
746745 const toolResult : ToolResult = result
747746 toolResults . push ( toolResult )
748747 }
749-
750- if ( toolUse . name === ToolType . FsWrite ) {
751- await vscode . commands . executeCommand (
752- 'vscode.open' ,
753- vscode . Uri . file ( ( toolUse . input as unknown as FsWriteParams ) . path )
754- )
755- }
756748 }
757749
758750 await this . generateResponse (
@@ -787,11 +779,26 @@ export class ChatController {
787779 } )
788780 }
789781
790- private async closeDiffView ( ) {
791- // Close the diff view if User reject the generated code changes.
782+ private async closeDiffView ( message : CustomFormActionMessage ) {
783+ // Close the diff view if User rejected or accepted the generated code changes.
792784 if ( vscode . window . tabGroups . activeTabGroup . activeTab ?. label . includes ( amazonQTabSuffix ) ) {
793785 await vscode . commands . executeCommand ( 'workbench.action.closeActiveEditor' )
794786 }
787+ // clean up temp file
788+ const tabID = message . tabID
789+ const toolUseId = message . action . formItemValues ?. toolUseId
790+ if ( ! tabID || ! toolUseId ) {
791+ return
792+ }
793+
794+ const session = this . sessionStorage . getSession ( tabID )
795+ const { filePath } = session . fsWriteBackups . get ( toolUseId ) ?? { }
796+ if ( filePath ) {
797+ const tempFilePath = await this . getTempFilePath ( filePath )
798+ if ( await fs . existsFile ( tempFilePath ) ) {
799+ await fs . delete ( tempFilePath )
800+ }
801+ }
795802 }
796803
797804 private async rejectShellCommand ( message : CustomFormActionMessage ) {
@@ -825,11 +832,11 @@ export class ChatController {
825832 await this . processToolUseMessage ( message )
826833 break
827834 case 'accept-code-diff' :
828- await this . closeDiffView ( )
835+ await this . closeDiffView ( message )
829836 break
830837 case 'reject-code-diff' :
831838 await this . restoreBackup ( message )
832- await this . closeDiffView ( )
839+ await this . closeDiffView ( message )
833840 break
834841 case 'reject-shell-command' :
835842 await this . rejectShellCommand ( message )
@@ -873,6 +880,21 @@ export class ChatController {
873880 }
874881 }
875882
883+ private async getTempFilePath ( filePath : string ) {
884+ // Create a temporary file path to show the diff view
885+ const pathToArchiveDir = path . join ( tempDirPath , 'q-chat' )
886+ const archivePathExists = await fs . existsDir ( pathToArchiveDir )
887+ if ( ! archivePathExists ) {
888+ await fs . mkdir ( pathToArchiveDir )
889+ }
890+ const resultArtifactsDir = path . join ( pathToArchiveDir , 'resultArtifacts' )
891+ const resultArtifactsDirExists = await fs . existsDir ( resultArtifactsDir )
892+ if ( ! resultArtifactsDirExists ) {
893+ await fs . mkdir ( resultArtifactsDir )
894+ }
895+ return path . join ( resultArtifactsDir , `temp-${ path . basename ( filePath ) } ` )
896+ }
897+
876898 private async processFileClickMessage ( message : FileClick ) {
877899 const session = this . sessionStorage . getSession ( message . tabID )
878900 // Check if user clicked on filePath in the contextList or in the fileListTree and perform the functionality accordingly.
@@ -884,18 +906,7 @@ export class ChatController {
884906 }
885907
886908 try {
887- // Create a temporary file path to show the diff view
888- // TODO: Use amazonQDiffScheme for temp file
889- const pathToArchiveDir = path . join ( tempDirPath , 'q-chat' )
890- const archivePathExists = await fs . existsDir ( pathToArchiveDir )
891- if ( archivePathExists ) {
892- await fs . delete ( pathToArchiveDir , { recursive : true } )
893- }
894- await fs . mkdir ( pathToArchiveDir )
895- const resultArtifactsDir = path . join ( pathToArchiveDir , 'resultArtifacts' )
896- await fs . mkdir ( resultArtifactsDir )
897-
898- const tempFilePath = path . join ( resultArtifactsDir , `temp-${ path . basename ( filePath ) } ` )
909+ const tempFilePath = await this . getTempFilePath ( filePath )
899910 await fs . writeFile ( tempFilePath , content )
900911
901912 const leftUri = vscode . Uri . file ( tempFilePath )
0 commit comments