|
14 | 14 | from codeflash.discovery.functions_to_optimize import filter_functions, get_functions_within_git_diff |
15 | 15 | from codeflash.either import is_successful |
16 | 16 | from codeflash.lsp.server import CodeflashLanguageServer, CodeflashLanguageServerProtocol |
17 | | -from codeflash.result.explanation import Explanation |
18 | 17 |
|
19 | 18 | if TYPE_CHECKING: |
20 | 19 | from lsprotocol import types |
21 | 20 |
|
22 | | - from codeflash.models.models import GeneratedTestsList, OptimizationSet |
23 | | - |
24 | 21 |
|
25 | 22 | @dataclass |
26 | 23 | class OptimizableFunctionsParams: |
@@ -179,67 +176,6 @@ def provide_api_key(server: CodeflashLanguageServer, params: ProvideApiKeyParams |
179 | 176 | return {"status": "error", "message": "something went wrong while saving the api key"} |
180 | 177 |
|
181 | 178 |
|
182 | | -@server.feature("prepareOptimization") |
183 | | -def prepare_optimization(server: CodeflashLanguageServer, params: FunctionOptimizationParams) -> dict[str, str]: |
184 | | - current_function = server.optimizer.current_function_being_optimized |
185 | | - |
186 | | - module_prep_result = server.optimizer.prepare_module_for_optimization(current_function.file_path) |
187 | | - validated_original_code, original_module_ast = module_prep_result |
188 | | - |
189 | | - function_optimizer = server.optimizer.create_function_optimizer( |
190 | | - current_function, |
191 | | - function_to_optimize_source_code=validated_original_code[current_function.file_path].source_code, |
192 | | - original_module_ast=original_module_ast, |
193 | | - original_module_path=current_function.file_path, |
194 | | - ) |
195 | | - |
196 | | - server.optimizer.current_function_optimizer = function_optimizer |
197 | | - if not function_optimizer: |
198 | | - return {"functionName": params.functionName, "status": "error", "message": "No function optimizer found"} |
199 | | - |
200 | | - initialization_result = function_optimizer.can_be_optimized() |
201 | | - if not is_successful(initialization_result): |
202 | | - return {"functionName": params.functionName, "status": "error", "message": initialization_result.failure()} |
203 | | - |
204 | | - return {"functionName": params.functionName, "status": "success", "message": "Optimization preparation completed"} |
205 | | - |
206 | | - |
207 | | -@server.feature("generateTests") |
208 | | -def generate_tests(server: CodeflashLanguageServer, params: FunctionOptimizationParams) -> dict[str, str]: |
209 | | - function_optimizer = server.optimizer.current_function_optimizer |
210 | | - if not function_optimizer: |
211 | | - return {"functionName": params.functionName, "status": "error", "message": "No function optimizer found"} |
212 | | - |
213 | | - initialization_result = function_optimizer.can_be_optimized() |
214 | | - if not is_successful(initialization_result): |
215 | | - return {"functionName": params.functionName, "status": "error", "message": initialization_result.failure()} |
216 | | - |
217 | | - should_run_experiment, code_context, original_helper_code = initialization_result.unwrap() |
218 | | - |
219 | | - test_setup_result = function_optimizer.generate_and_instrument_tests( |
220 | | - code_context, should_run_experiment=should_run_experiment |
221 | | - ) |
222 | | - if not is_successful(test_setup_result): |
223 | | - return {"functionName": params.functionName, "status": "error", "message": test_setup_result.failure()} |
224 | | - generated_tests_list: GeneratedTestsList |
225 | | - optimizations_set: OptimizationSet |
226 | | - generated_tests_list, _, concolic__test_str, optimizations_set = test_setup_result.unwrap() |
227 | | - |
228 | | - generated_tests: list[str] = [ |
229 | | - generated_test.generated_original_test_source for generated_test in generated_tests_list.generated_tests |
230 | | - ] |
231 | | - optimizations_dict = { |
232 | | - candidate.optimization_id: {"source_code": candidate.source_code.markdown, "explanation": candidate.explanation} |
233 | | - for candidate in optimizations_set.control + optimizations_set.experiment |
234 | | - } |
235 | | - |
236 | | - return { |
237 | | - "functionName": params.functionName, |
238 | | - "status": "success", |
239 | | - "message": {"generated_tests": generated_tests, "optimizations": optimizations_dict}, |
240 | | - } |
241 | | - |
242 | | - |
243 | 179 | @server.feature("performFunctionOptimization") |
244 | 180 | def perform_function_optimization( # noqa: PLR0911 |
245 | 181 | server: CodeflashLanguageServer, params: FunctionOptimizationParams |
@@ -351,16 +287,14 @@ def perform_function_optimization( # noqa: PLR0911 |
351 | 287 |
|
352 | 288 | server.show_message_log(f"Optimization completed for {params.functionName} with {speedup:.2f}x speedup", "Info") |
353 | 289 |
|
354 | | - explanation = best_optimization.candidate.explanation |
355 | | - explanation_str = explanation.explanation_message() if isinstance(explanation, Explanation) else explanation |
356 | 290 | return { |
357 | 291 | "functionName": params.functionName, |
358 | 292 | "status": "success", |
359 | 293 | "message": "Optimization completed successfully", |
360 | 294 | "extra": f"Speedup: {speedup:.2f}x faster", |
361 | 295 | "optimization": optimized_source, |
362 | 296 | "patch_file": str(patch_file), |
363 | | - "explanation": explanation_str, |
| 297 | + "explanation": best_optimization.explanation_v2, |
364 | 298 | } |
365 | 299 | finally: |
366 | 300 | cleanup_the_optimizer(server) |
|
0 commit comments