1+ use crate :: plugins:: deno:: error:: { PluginError , Result } ;
2+ use crate :: utils:: gen:: generate_id;
13use serde:: { Deserialize , Serialize } ;
24use serde_json:: Value ;
3- use crate :: plugins:: deno:: error:: { PluginError , Result } ;
4- use base64:: engine:: { general_purpose:: STANDARD , Engine } ;
5+ use std:: fs:: { create_dir_all, write} ;
56
67/// 插件信息结构
78#[ derive( Debug , Serialize , Deserialize , Clone ) ]
@@ -47,48 +48,55 @@ impl PluginManager {
4748 /// 执行插件工具
4849 pub async fn execute ( & self , content : & str , tool : & str , args : Value ) -> Result < Value > {
4950 let runtime = crate :: plugins:: deno:: DENO_RUNTIME . lock ( ) . await ;
50- let runtime = runtime. as_ref ( ) . ok_or_else ( || PluginError :: Plugin ( "Deno运行时未初始化" . to_string ( ) ) ) ?;
51+ let runtime = runtime
52+ . as_ref ( )
53+ . ok_or_else ( || PluginError :: Plugin ( "Deno运行时未初始化" . to_string ( ) ) ) ?;
5154
5255 if !runtime. check_installed ( ) {
5356 return Err ( PluginError :: DenoNotInstalled ) ;
5457 }
5558
56- // 使用 data URL 直接执行内容
59+ // 获取插件目录
60+ let config_dir = crate :: utils:: file:: get_config_dir ( )
61+ . ok_or_else ( || PluginError :: Plugin ( "无法获取配置目录" . to_string ( ) ) ) ?;
62+ let plugins_dir = config_dir. join ( "plugins" ) ;
63+ create_dir_all ( & plugins_dir) ?;
64+
65+ // 创建唯一的插件文件
66+ let plugin_path = plugins_dir. join ( format ! ( "plugin_{}.ts" , generate_id( ) ) ) ;
67+ let plugin_path_str = plugin_path. to_str ( ) . unwrap ( ) ;
68+
69+ // 写入插件内容
70+ write ( & plugin_path, content) ?;
71+
72+ // 使用文件路径方式执行
5773 let script = format ! (
5874 r#"
5975 (async () => {{
60- const plugin = await import('data:text/typescript;base64,{content }');
76+ const plugin = await import('file://{plugin_path }');
6177 const targetFunction = plugin['{tool}'];
6278 if (typeof targetFunction !== 'function') {{
6379 throw new Error(`插件中未找到函数 '{tool}' 或导出不是一个函数。`);
6480 }}
65- // 从 Rust 传递过来的 JSON 字符串,需要解析
6681 const parsedArgs = JSON.parse('{args_json}');
67- // 直接调用目标函数并传递解析后的参数对象
68- // TypeScript 函数内部负责处理参数结构
6982 const result = await targetFunction(parsedArgs);
70- // 将结果序列化为 JSON 字符串并打印到 stdout, 以便 Rust 捕获
7183 console.log(JSON.stringify(result !== undefined ? result : null));
7284 }})();
7385 "# ,
74- content = STANDARD . encode ( content ) ,
86+ plugin_path = plugin_path_str . replace ( " \\ " , "/" ) , // 确保路径使用正斜杠
7587 tool = tool,
76- // 确保 args 被正确序列化为 JSON 字符串, 并进行 JS 字符串所需的基本转义
7788 args_json = serde_json:: to_string( & args) ?
78- . replace( "\\ " , "\\ \\ " ) // 必须先转义反斜杠本身
79- . replace( "'" , "\\ '" ) // 转义单引号
80- . replace( "\" " , "\\ \" " ) // 转义双引号
81- . replace( "\n " , "\\ n" ) // 转义换行符
82- . replace( "\r " , "\\ r" ) // 转义回车符
89+ . replace( "\\ " , "\\ \\ " )
90+ . replace( "'" , "\\ '" )
91+ . replace( "\" " , "\\ \" " )
92+ . replace( "\n " , "\\ n" )
93+ . replace( "\r " , "\\ r" )
8394 ) ;
8495
85- println ! ( "{}" , script) ;
86-
8796 let env_vars = self . env_manager . load ( ) . await ?;
8897 let output = runtime. execute ( & script, & env_vars) . await ?;
89-
98+ // 清理插件文件
99+ let _ = std:: fs:: remove_file ( & plugin_path) ;
90100 serde_json:: from_str ( & output) . map_err ( |e| PluginError :: Json ( e. to_string ( ) ) )
91101 }
92-
93-
94- }
102+ }
0 commit comments