@@ -120,24 +120,28 @@ def _get_valid_candidates(
120120 )
121121 return candidates
122122
123- def optimize_python_code ( # noqa: D417
123+ def optimize_code ( # noqa: D417
124124 self ,
125125 source_code : str ,
126126 dependency_code : str ,
127127 trace_id : str ,
128128 experiment_metadata : ExperimentMetadata | None = None ,
129129 * ,
130+ language : str = "python" ,
131+ language_version : str | None = None ,
130132 is_async : bool = False ,
131133 n_candidates : int = 5 ,
132134 ) -> list [OptimizedCandidate ]:
133- """Optimize the given python code for performance by making a request to the Django endpoint.
135+ """Optimize the given code for performance by making a request to the Django endpoint.
134136
135137 Parameters
136138 ----------
137- - source_code (str): The python code to optimize.
139+ - source_code (str): The code to optimize.
138140 - dependency_code (str): The dependency code used as read-only context for the optimization
139141 - trace_id (str): Trace id of optimization run
140142 - experiment_metadata (Optional[ExperimentalMetadata, None]): Any available experiment metadata for this optimization
143+ - language (str): Programming language ("python", "javascript", "typescript")
144+ - language_version (str | None): Language version (e.g., "3.11.0" for Python, "ES2022" for JS)
141145 - is_async (bool): Whether the function being optimized is async
142146 - n_candidates (int): Number of candidates to generate
143147
@@ -151,11 +155,12 @@ def optimize_python_code( # noqa: D417
151155 start_time = time .perf_counter ()
152156 git_repo_owner , git_repo_name = safe_get_repo_owner_and_name ()
153157
154- payload = {
158+ # Build payload with language-specific fields
159+ payload : dict [str , Any ] = {
155160 "source_code" : source_code ,
156161 "dependency_code" : dependency_code ,
157162 "trace_id" : trace_id ,
158- "python_version " : platform . python_version () ,
163+ "language " : language ,
159164 "experiment_metadata" : experiment_metadata ,
160165 "codeflash_version" : codeflash_version ,
161166 "current_username" : get_last_commit_author_if_pr_exists (None ),
@@ -165,6 +170,14 @@ def optimize_python_code( # noqa: D417
165170 "call_sequence" : self .get_next_sequence (),
166171 "n_candidates" : n_candidates ,
167172 }
173+
174+ # Add language-specific version fields
175+ # Always include python_version for backward compatibility with older backend
176+ payload ["python_version" ] = platform .python_version ()
177+ if language == "python" :
178+ pass # python_version already set
179+ else :
180+ payload ["language_version" ] = language_version or "ES2022"
168181 logger .debug (f"Sending optimize request: trace_id={ trace_id } , n_candidates={ payload ['n_candidates' ]} " )
169182
170183 try :
@@ -191,6 +204,28 @@ def optimize_python_code( # noqa: D417
191204 console .rule ()
192205 return []
193206
207+ # Backward-compatible alias
208+ def optimize_python_code (
209+ self ,
210+ source_code : str ,
211+ dependency_code : str ,
212+ trace_id : str ,
213+ experiment_metadata : ExperimentMetadata | None = None ,
214+ * ,
215+ is_async : bool = False ,
216+ n_candidates : int = 5 ,
217+ ) -> list [OptimizedCandidate ]:
218+ """Backward-compatible alias for optimize_code() with language='python'."""
219+ return self .optimize_code (
220+ source_code = source_code ,
221+ dependency_code = dependency_code ,
222+ trace_id = trace_id ,
223+ experiment_metadata = experiment_metadata ,
224+ language = "python" ,
225+ is_async = is_async ,
226+ n_candidates = n_candidates ,
227+ )
228+
194229 def optimize_python_code_line_profiler ( # noqa: D417
195230 self ,
196231 source_code : str ,
@@ -578,6 +613,9 @@ def generate_regression_tests( # noqa: D417
578613 test_timeout : int ,
579614 trace_id : str ,
580615 test_index : int ,
616+ * ,
617+ language : str = "python" ,
618+ language_version : str | None = None ,
581619 ) -> tuple [str , str , str ] | None :
582620 """Generate regression tests for the given function by making a request to the Django endpoint.
583621
@@ -588,19 +626,30 @@ def generate_regression_tests( # noqa: D417
588626 - helper_function_names (list[Source]): List of helper function names.
589627 - module_path (Path): The module path where the function is located.
590628 - test_module_path (Path): The module path for the test code.
591- - test_framework (str): The test framework to use, e.g., "pytest".
629+ - test_framework (str): The test framework to use, e.g., "pytest", "jest" .
592630 - test_timeout (int): The timeout for each test in seconds.
593631 - test_index (int): The index from 0-(n-1) if n tests are generated for a single trace_id
632+ - language (str): Programming language ("python", "javascript", "typescript")
633+ - language_version (str | None): Language version (e.g., "3.11.0" for Python, "ES2022" for JS)
594634
595635 Returns
596636 -------
597637 - Dict[str, str] | None: The generated regression tests and instrumented tests, or None if an error occurred.
598638
599639 """
600- assert test_framework in ["pytest" , "unittest" ], (
601- f"Invalid test framework, got { test_framework } but expected 'pytest' or 'unittest'"
602- )
603- payload = {
640+ # Validate test framework based on language
641+ python_frameworks = ["pytest" , "unittest" ]
642+ javascript_frameworks = ["jest" , "mocha" , "vitest" ]
643+ if language == "python" :
644+ assert test_framework in python_frameworks , (
645+ f"Invalid test framework for Python, got { test_framework } but expected one of { python_frameworks } "
646+ )
647+ elif language in ("javascript" , "typescript" ):
648+ assert test_framework in javascript_frameworks , (
649+ f"Invalid test framework for JavaScript, got { test_framework } but expected one of { javascript_frameworks } "
650+ )
651+
652+ payload : dict [str , Any ] = {
604653 "source_code_being_tested" : source_code_being_tested ,
605654 "function_to_optimize" : function_to_optimize ,
606655 "helper_function_names" : helper_function_names ,
@@ -610,11 +659,19 @@ def generate_regression_tests( # noqa: D417
610659 "test_timeout" : test_timeout ,
611660 "trace_id" : trace_id ,
612661 "test_index" : test_index ,
613- "python_version " : platform . python_version () ,
662+ "language " : language ,
614663 "codeflash_version" : codeflash_version ,
615664 "is_async" : function_to_optimize .is_async ,
616665 "call_sequence" : self .get_next_sequence (),
617666 }
667+
668+ # Add language-specific version fields
669+ # Always include python_version for backward compatibility with older backend
670+ payload ["python_version" ] = platform .python_version ()
671+ if language == "python" :
672+ pass # python_version already set
673+ else :
674+ payload ["language_version" ] = language_version or "ES2022"
618675 try :
619676 response = self .make_ai_service_request ("/testgen" , payload = payload , timeout = self .timeout )
620677 except requests .exceptions .RequestException as e :
0 commit comments