@@ -3,7 +3,7 @@ import * as path from 'path';
33import * as fs from 'fs' ;
44import { selectFiles } from './fileSelector' ;
55import { generateCvb , applyCvbToWorkspace , generateTimestamp , Cvb , TCVB , mergeCvb } from './cvbManager' ;
6- import { queryCodeReDesign , generateFilenameFromRequest , analyzeCode } from './deepseekApi' ;
6+ import { queryCodeReDesign , generateFilenameFromRequest , analyzeCode , callDeepSeekFixApi } from './deepseekApi' ;
77import { setupCvbAsMarkdown } from './cvbMarkdownHandler' ;
88import { registerCvbContextMenu } from './siderBar' ;
99
@@ -31,6 +31,55 @@ export function clearCurrentOperationController() {
3131 }
3232}
3333
34+ export async function doUploadCommand ( cvbFilePath : string , userPrompt : string , outputChannel : vscode . OutputChannel ) {
35+ const workspaceFolders = vscode . workspace . workspaceFolders ;
36+ if ( ! workspaceFolders ) {
37+ vscode . window . showErrorMessage ( 'No workspace folder found' ) ;
38+ return ;
39+ }
40+
41+ const workspacePath = workspaceFolders [ 0 ] . uri . fsPath ;
42+ const tmpDir = path . join ( workspacePath , '.CodeReDesignWorkSpace' ) ;
43+
44+ const filenameSummary = await generateFilenameFromRequest ( userPrompt ) ;
45+ const timestamp = generateTimestamp ( ) ;
46+ let baseFileName = `${ timestamp } _${ filenameSummary } .cvb` ;
47+ let fileName = baseFileName ;
48+ let i = 1 ;
49+ while ( fs . existsSync ( path . join ( tmpDir , fileName ) ) ) {
50+ fileName = `${ timestamp } _${ filenameSummary } _${ i } .cvb` ;
51+ i ++ ;
52+ }
53+
54+ const cvbContent = fs . readFileSync ( cvbFilePath , 'utf-8' ) ;
55+
56+ resetCurrentOperationController ( ) ;
57+
58+ let apiResponse = await queryCodeReDesign ( cvbContent , userPrompt , outputChannel , getCurrentOperationController ( ) . signal ) ;
59+ let processSuccess = true ;
60+ do {
61+ try {
62+ if ( apiResponse ) {
63+ const tcvb = new TCVB ( apiResponse ) ;
64+ const oldCvb = new Cvb ( cvbContent ) ;
65+ const cvb = mergeCvb ( oldCvb , tcvb ) ;
66+
67+ processSuccess = true ;
68+
69+ cvb . setMetaData ( "用户需求" , userPrompt ) ;
70+ const newCvbFilePath = path . join ( tmpDir , fileName ) ;
71+ fs . writeFileSync ( newCvbFilePath , cvb . toString ( ) , 'utf-8' ) ;
72+ vscode . window . showInformationMessage ( `API response saved as CVB file: ${ newCvbFilePath } ` ) ;
73+ }
74+ } catch ( err : any ) {
75+ apiResponse = await callDeepSeekFixApi ( err . message , outputChannel , true , getCurrentOperationController ( ) . signal ) ;
76+ processSuccess = false ;
77+ }
78+ } while ( ! processSuccess ) ;
79+
80+ clearCurrentOperationController ( ) ;
81+ }
82+
3483// 插件激活时调用
3584export function activate ( context : vscode . ExtensionContext ) {
3685 console . log ( 'Congratulations, your extension "CodeReDesign" is now active!' ) ;
@@ -98,33 +147,10 @@ export function activate(context: vscode.ExtensionContext) {
98147 if ( ! userPrompt ) {
99148 return ;
100149 }
101-
102- const filenameSummary = await generateFilenameFromRequest ( userPrompt ) ;
103- const timestamp = generateTimestamp ( ) ;
104- let baseFileName = `${ timestamp } _${ filenameSummary } .cvb` ;
105- let fileName = baseFileName ;
106- let i = 1 ;
107- while ( fs . existsSync ( path . join ( tmpDir , fileName ) ) ) {
108- fileName = `${ timestamp } _${ filenameSummary } _${ i } .cvb` ;
109- i ++ ;
110- }
111-
112- const cvbFilePath = path . join ( tmpDir , selectedCvbFile ) ;
113- const cvbContent = fs . readFileSync ( cvbFilePath , 'utf-8' ) ;
114-
115- resetCurrentOperationController ( ) ;
116150
117- const apiResponse = await queryCodeReDesign ( cvbContent , userPrompt , outputChannel , getCurrentOperationController ( ) . signal ) ;
118- if ( apiResponse ) {
119- const tcvb = new TCVB ( apiResponse ) ;
120- const oldCvb = new Cvb ( cvbContent ) ;
121- const cvb = mergeCvb ( oldCvb , tcvb ) ;
122- cvb . setMetaData ( "用户需求" , userPrompt ) ;
123- const newCvbFilePath = path . join ( tmpDir , fileName ) ;
124- fs . writeFileSync ( newCvbFilePath , cvb . toString ( ) , 'utf-8' ) ;
125- vscode . window . showInformationMessage ( `API response saved as CVB file: ${ newCvbFilePath } ` ) ;
126- }
127- clearCurrentOperationController ( ) ;
151+ const cvbFilePath = path . join ( tmpDir , selectedCvbFile ) ;
152+
153+ doUploadCommand ( cvbFilePath , userPrompt , outputChannel ) ;
128154 } ) ;
129155
130156 // 注册命令:中断当前的上传操作
0 commit comments