@@ -207,6 +207,154 @@ def get_code_optimization_context(
207207 preexisting_objects = preexisting_objects ,
208208 )
209209
210+ def get_code_optimization_context_for_language (
211+ function_to_optimize : FunctionToOptimize ,
212+ project_root_path : Path ,
213+ optim_token_limit : int = OPTIMIZATION_CONTEXT_TOKEN_LIMIT ,
214+ testgen_token_limit : int = TESTGEN_CONTEXT_TOKEN_LIMIT ,
215+ ) -> CodeOptimizationContext :
216+ """Extract code optimization context for non-Python languages.
217+
218+ Uses the language support abstraction to extract code context and converts
219+ it to the CodeOptimizationContext format expected by the pipeline.
220+
221+ This function supports multi-file context extraction, grouping helpers by file
222+ and creating proper CodeStringsMarkdown with file paths for multi-file replacement.
223+
224+ Args:
225+ function_to_optimize: The function to extract context for.
226+ project_root_path: Root of the project.
227+ optim_token_limit: Token limit for optimization context.
228+ testgen_token_limit: Token limit for testgen context.
229+
230+ Returns:
231+ CodeOptimizationContext with target code and dependencies.
232+
233+ """
234+ from codeflash .languages import get_language_support
235+ from codeflash .languages .base import FunctionInfo , ParentInfo
236+
237+ # Get language support for this function
238+ language = Language (function_to_optimize .language )
239+ lang_support = get_language_support (language )
240+
241+ # Convert FunctionToOptimize to FunctionInfo for language support
242+ parents = tuple (ParentInfo (name = p .name , type = p .type ) for p in function_to_optimize .parents )
243+ func_info = FunctionInfo (
244+ name = function_to_optimize .function_name ,
245+ file_path = function_to_optimize .file_path ,
246+ start_line = function_to_optimize .starting_line or 1 ,
247+ end_line = function_to_optimize .ending_line or 1 ,
248+ parents = parents ,
249+ is_async = function_to_optimize .is_async ,
250+ is_method = len (function_to_optimize .parents ) > 0 ,
251+ language = language ,
252+ )
253+
254+ # Extract code context using language support
255+ code_context = lang_support .extract_code_context (func_info , project_root_path , project_root_path )
256+
257+ # Build imports string if available
258+ imports_code = "\n " .join (code_context .imports ) if code_context .imports else ""
259+
260+ # Get relative path for target file
261+ try :
262+ target_relative_path = function_to_optimize .file_path .resolve ().relative_to (project_root_path .resolve ())
263+ except ValueError :
264+ target_relative_path = function_to_optimize .file_path
265+
266+ # Group helpers by file path
267+ helpers_by_file : dict [Path , list ] = defaultdict (list )
268+ helper_function_sources = []
269+
270+ for helper in code_context .helper_functions :
271+ helpers_by_file [helper .file_path ].append (helper )
272+
273+ # Convert to FunctionSource for pipeline compatibility
274+ helper_function_sources .append (
275+ FunctionSource (
276+ file_path = helper .file_path ,
277+ qualified_name = helper .qualified_name ,
278+ fully_qualified_name = helper .qualified_name ,
279+ only_function_name = helper .name ,
280+ source_code = helper .source_code ,
281+ jedi_definition = None ,
282+ )
283+ )
284+
285+ # Build read-writable code (target file + same-file helpers + global variables)
286+ read_writable_code_strings = []
287+
288+ # Combine target code with same-file helpers
289+ target_file_code = code_context .target_code
290+ same_file_helpers = helpers_by_file .get (function_to_optimize .file_path , [])
291+ if same_file_helpers :
292+ helper_code = "\n \n " .join (h .source_code for h in same_file_helpers )
293+ target_file_code = target_file_code + "\n \n " + helper_code
294+
295+ # Add global variables (module-level declarations) referenced by the function and helpers
296+ # These should be included in read-writable context so AI can modify them if needed
297+ if code_context .read_only_context :
298+ target_file_code = code_context .read_only_context + "\n \n " + target_file_code
299+
300+ # Add imports to target file code
301+ if imports_code :
302+ target_file_code = imports_code + "\n \n " + target_file_code
303+
304+ read_writable_code_strings .append (
305+ CodeString (code = target_file_code , file_path = target_relative_path , language = function_to_optimize .language )
306+ )
307+
308+ # Add helper files (cross-file helpers)
309+ for file_path , file_helpers in helpers_by_file .items ():
310+ if file_path == function_to_optimize .file_path :
311+ continue # Already included in target file
312+
313+ try :
314+ helper_relative_path = file_path .resolve ().relative_to (project_root_path .resolve ())
315+ except ValueError :
316+ helper_relative_path = file_path
317+
318+ # Combine all helpers from this file
319+ combined_helper_code = "\n \n " .join (h .source_code for h in file_helpers )
320+
321+ read_writable_code_strings .append (
322+ CodeString (
323+ code = combined_helper_code , file_path = helper_relative_path , language = function_to_optimize .language
324+ )
325+ )
326+
327+ read_writable_code = CodeStringsMarkdown (
328+ code_strings = read_writable_code_strings , language = function_to_optimize .language
329+ )
330+
331+ # Build testgen context (same as read_writable for non-Python)
332+ testgen_context = CodeStringsMarkdown (
333+ code_strings = read_writable_code_strings .copy (), language = function_to_optimize .language
334+ )
335+
336+ # Check token limits
337+ read_writable_tokens = encoded_tokens_len (read_writable_code .markdown )
338+ if read_writable_tokens > optim_token_limit :
339+ raise ValueError ("Read-writable code has exceeded token limit, cannot proceed" )
340+
341+ testgen_tokens = encoded_tokens_len (testgen_context .markdown )
342+ if testgen_tokens > testgen_token_limit :
343+ raise ValueError ("Testgen code context has exceeded token limit, cannot proceed" )
344+
345+ # Generate code hash from all read-writable code
346+ code_hash = hashlib .sha256 (read_writable_code .flat .encode ("utf-8" )).hexdigest ()
347+
348+ return CodeOptimizationContext (
349+ testgen_context = testgen_context ,
350+ read_writable_code = read_writable_code ,
351+ # Global variables are now included in read-writable code, so don't duplicate in read-only
352+ read_only_context_code = "" ,
353+ hashing_code_context = read_writable_code .flat ,
354+ hashing_code_context_hash = code_hash ,
355+ helper_functions = helper_function_sources ,
356+ preexisting_objects = set (), # Not implemented for non-Python yet
357+ )
210358
211359def extract_code_markdown_context_from_files (
212360 helpers_of_fto : dict [Path , set [FunctionSource ]],
0 commit comments