@@ -10,16 +10,21 @@ import * as os from 'os';
1010let EXTENSION_PATH : string = '' ;
1111
1212const isDevMode = __dirname . includes ( 'dist' ) ;
13- const pythonScriptPath = isDevMode
14- ? path . join ( __dirname , '..' , 'src' , 'python' , 'rag.py' ) // Navigate from dist to src in dev
15- : path . join ( EXTENSION_PATH , 'src' , 'python' , 'rag.py' ) ;
13+ // 动态设置 Python 脚本或 EXE 路径
14+ const getPythonScriptPath = ( extensionPath : string ) => {
15+ if ( isDevMode ) {
16+ return path . join ( extensionPath , 'src' , 'python' , 'rag.py' ) ; // 开发模式:直接运行 Python 脚本
17+ } else {
18+ return path . join ( extensionPath , 'dist' , 'rag.exe' ) ; // 发布模式:运行 EXE
19+ }
20+ } ;
1621
1722// 配置常量 (use a function to resolve paths dynamically)
1823export const CONFIG = {
1924 STORAGE_PATH : path . join ( os . homedir ( ) , 'CodeReDesignMemory' , 'rag_storage' ) ,
2025 PORT : 7111 ,
2126 LOCK_FILE : 'rag.lock' ,
22- PYTHON_SCRIPT : pythonScriptPath
27+ PYTHON_SCRIPT : getPythonScriptPath ( EXTENSION_PATH )
2328 } ;
2429
2530// 类型定义
@@ -175,22 +180,39 @@ class RagService {
175180 }
176181
177182 private startPythonProcess ( ) : ChildProcess {
178- const pythonPath = process . platform === 'win32'
179- ? this . findWindowsPython ( )
180- : this . findUnixPython ( ) ;
181-
182- // 通过命令行参数传递配置
183- const pythonArgs = [
184- CONFIG . PYTHON_SCRIPT ,
185- `--port=${ CONFIG . PORT } ` , // 传递端口参数
186- `--storage_path=${ CONFIG . STORAGE_PATH } ` // 可选:传递其他参数
187- ] ;
183+ const scriptPath = CONFIG . PYTHON_SCRIPT ;
184+ const isExe = scriptPath . endsWith ( '.exe' ) ;
185+
186+ if ( ! fs . access ( scriptPath ) ) {
187+ throw new Error ( `未找到脚本或可执行文件: ${ scriptPath } ` ) ;
188+ }
188189
189- return spawn ( pythonPath , pythonArgs , {
190- env : { ...process . env , PORT : CONFIG . PORT . toString ( ) } ,
191- stdio : 'pipe' ,
192- shell : true // 启用shell解析路径
193- } ) ;
190+ if ( isExe ) {
191+ // 直接运行 EXE
192+ return spawn ( scriptPath , [
193+ `--port=${ CONFIG . PORT } ` ,
194+ `--storage_path=${ CONFIG . STORAGE_PATH } `
195+ ] , {
196+ env : { ...process . env , PORT : CONFIG . PORT . toString ( ) } ,
197+ stdio : 'pipe' ,
198+ shell : true
199+ } ) ;
200+ } else {
201+ // 运行 Python 脚本
202+ const pythonPath = process . platform === 'win32'
203+ ? this . findWindowsPython ( )
204+ : this . findUnixPython ( ) ;
205+
206+ return spawn ( pythonPath , [
207+ scriptPath ,
208+ `--port=${ CONFIG . PORT } ` ,
209+ `--storage_path=${ CONFIG . STORAGE_PATH } `
210+ ] , {
211+ env : { ...process . env , PORT : CONFIG . PORT . toString ( ) } ,
212+ stdio : 'pipe' ,
213+ shell : true
214+ } ) ;
215+ }
194216 }
195217
196218 private findUnixPython ( ) : string {
@@ -263,6 +285,7 @@ class RagService {
263285export const ragService = RagService . getInstance ( ) ;
264286
265287export async function activate ( context : vscode . ExtensionContext ) {
288+ EXTENSION_PATH = context . extensionPath ;
266289 await ragService . start ( ) ;
267290 context . subscriptions . push ( {
268291 dispose : ( ) => ragService . stop ( )
0 commit comments