@@ -7,14 +7,36 @@ import { queryCodeReDesign, generateFilenameFromRequest, analyzeCode } from './d
77import { setupCvbAsMarkdown } from './cvbMarkdownHandler' ;
88import { registerCvbContextMenu } from './siderBar' ;
99
10+ let currentOperationController : AbortController | null = null ;
11+
12+ export function getCurrentOperationController ( ) {
13+ if ( ! currentOperationController ) {
14+ currentOperationController = new AbortController ;
15+ }
16+ return currentOperationController ;
17+ }
18+
19+ export function resetCurrentOperationController ( ) {
20+ if ( currentOperationController ) {
21+ currentOperationController . abort ( ) ;
22+ currentOperationController = null ;
23+ }
24+ currentOperationController = new AbortController ;
25+ }
26+
27+ export function clearCurrentOperationController ( ) {
28+ if ( currentOperationController ) {
29+ currentOperationController . abort ( ) ; // 中止操作
30+ currentOperationController = null ; // 清除引用
31+ }
32+ }
33+
1034// 插件激活时调用
1135export function activate ( context : vscode . ExtensionContext ) {
1236 console . log ( 'Congratulations, your extension "CodeReDesign" is now active!' ) ;
1337
1438 // 创建输出通道
1539 const outputChannel = vscode . window . createOutputChannel ( 'CodeReDesign API Stream' ) ;
16- // 用于存储当前的上传操作
17- let currentOperationController : AbortController | null = null ;
1840
1941 // 注册命令:选择文件并生成 CVB
2042 let generateCvbCommand = vscode . commands . registerCommand ( 'codeReDesign.generateCvb' , async ( ) => {
@@ -90,21 +112,16 @@ export function activate(context: vscode.ExtensionContext) {
90112 const cvbFilePath = path . join ( tmpDir , selectedCvbFile ) ;
91113 const cvbContent = fs . readFileSync ( cvbFilePath , 'utf-8' ) ;
92114
93- if ( currentOperationController ) {
94- currentOperationController . abort ( ) ;
95- currentOperationController = null ;
96- }
97-
98- // 创建新的 AbortController
99- currentOperationController = new AbortController ( ) ;
100- const apiResponse = await queryCodeReDesign ( cvbContent , userPrompt , outputChannel , currentOperationController . signal ) ;
115+ resetCurrentOperationController ( ) ;
116+
117+ const apiResponse = await queryCodeReDesign ( cvbContent , userPrompt , outputChannel , getCurrentOperationController ( ) . signal ) ;
101118 if ( apiResponse ) {
102119 const { cvbContent : newCvbContent , metadata, files } = parseCvb ( apiResponse ) ;
103120 const newCvbFilePath = path . join ( tmpDir , fileName ) ;
104121 fs . writeFileSync ( newCvbFilePath , newCvbContent , 'utf-8' ) ;
105122 vscode . window . showInformationMessage ( `API response saved as CVB file: ${ newCvbFilePath } ` ) ;
106123 }
107- currentOperationController = null ;
124+ clearCurrentOperationController ( ) ;
108125 } ) ;
109126
110127 // 注册命令:中断当前的上传操作
@@ -196,14 +213,9 @@ export function activate(context: vscode.ExtensionContext) {
196213 return ;
197214 }
198215
199- if ( currentOperationController ) {
200- currentOperationController . abort ( ) ;
201- currentOperationController = null ;
202- }
203- // 创建新的 AbortController
204- currentOperationController = new AbortController ( ) ;
216+ resetCurrentOperationController ( ) ;
205217 // 调用分析代码功能
206- const analysisResult = await analyzeCode ( cvbContent , userRequest , outputChannel , currentOperationController . signal ) ;
218+ const analysisResult = await analyzeCode ( cvbContent , userRequest , outputChannel , getCurrentOperationController ( ) . signal ) ;
207219 if ( analysisResult ) {
208220 vscode . window . showInformationMessage ( 'Analysis completed. Check the output channel for details.' ) ;
209221 }
0 commit comments