@@ -46,7 +46,11 @@ def input_schema(self) -> dict:
4646 },
4747 "code" : {
4848 "type" : "string" ,
49- "description" : "Interactor C++ 代码(基于 testlib.h)" ,
49+ "description" : "C++ 源代码(与 source_path 二选一)" ,
50+ },
51+ "source_path" : {
52+ "type" : "string" ,
53+ "description" : "源文件路径,相对于 problem_dir 或绝对路径。与 code 二选一,优先级高于 code" ,
5054 },
5155 "reference_solution_path" : {
5256 "type" : "string" ,
@@ -63,18 +67,43 @@ def input_schema(self) -> dict:
6367 "default" : "g++" ,
6468 },
6569 },
66- "required" : ["problem_dir" , "code" ],
70+ "required" : ["problem_dir" ],
71+ "anyOf" : [
72+ {"required" : ["code" ]},
73+ {"required" : ["source_path" ]},
74+ ],
6775 }
6876
6977 async def execute (
7078 self ,
7179 problem_dir : str ,
72- code : str ,
80+ code : str | None = None ,
81+ source_path : str | None = None ,
7382 reference_solution_path : str | None = None ,
7483 mutant_solutions : list [str ] | None = None ,
7584 compiler : str = "g++" ,
7685 ) -> ToolResult :
7786 """执行 Interactor 构建。"""
87+ # 解析源代码:source_path 优先于 code
88+ source_dir = None
89+ if source_path :
90+ if not os .path .isabs (source_path ):
91+ source_path = os .path .join (problem_dir , source_path )
92+ if not os .path .exists (source_path ):
93+ return ToolResult .fail (f"Source file not found: { source_path } " )
94+ try :
95+ with open (source_path , encoding = "utf-8" ) as f :
96+ code = f .read ()
97+ except UnicodeDecodeError :
98+ try :
99+ with open (source_path , encoding = "latin-1" ) as f :
100+ code = f .read ()
101+ except Exception as e :
102+ return ToolResult .fail (f"Failed to read source file: { e } " )
103+ source_dir = os .path .dirname (os .path .abspath (source_path ))
104+ elif code is None :
105+ return ToolResult .fail ("Either 'code' or 'source_path' must be provided" )
106+
78107 os .makedirs (problem_dir , exist_ok = True )
79108
80109 # 保存到 files/ 子目录
@@ -92,7 +121,8 @@ async def execute(
92121 # 编译
93122 binary_path = os .path .join (files_dir , f"interactor{ get_exe_extension ()} " )
94123
95- compile_result = await compile_cpp (source_path , binary_path , compiler = compiler )
124+ include_dirs = [source_dir ] if source_dir else None
125+ compile_result = await compile_cpp (source_path , binary_path , compiler = compiler , include_dirs = include_dirs )
96126
97127 if not compile_result .success :
98128 return ToolResult .fail (
0 commit comments