# codex_cli/explain.pyimportosfromrich.consoleimportConsolefromrich.markdownimportMarkdownfrom.core.openai_utilsimportget_openai_responseconsole=Console()# --- UPDATED SIGNATURE: Added detail and lang ---
[docs]defexplain_code(input_str:str,detail:str="basic",lang:str="en"):"""Explains a code snippet, shell command, or the content of a file."""content_to_explain=""is_file=Falseread_error=Falseifos.path.isfile(input_str):is_file=Truetry:withopen(input_str,'r')asf:content_to_explain=f.read()console.print(f"Explaining content from file: {input_str}")exceptExceptionase:console.print(f"[bold red]Error reading file {input_str}: {e}[/bold red]")read_error=Truereturnelse:content_to_explain=input_strifread_error:return# Exit if reading failedifnotcontent_to_explain:console.print("[bold red]Cannot explain empty content.[/bold red]")return# --- UPDATED PROMPT CONSTRUCTION ---prompt_type="content from a file"ifis_fileelse"code snippet or shell command"# Determine detail level instruction based on the optiondetail_instruction="Provide a detailed, in-depth explanation."ifdetail.lower()=="detailed"else"Provide a clear and concise explanation."# Specify the desired languagelanguage_instruction=f"Respond ONLY in the following language: {lang}."prompt=f""" Your task is to explain the following {prompt_type}.{detail_instruction} Explain its purpose and key parts. Use Markdown for formatting. Make sure your entire response is {language_instruction} ```{content_to_explain} ``` """# --- END UPDATED PROMPT ---explanation=get_openai_response(prompt)ifexplanation:ifisinstance(explanation,str):console.print("\n✨ [bold green]Explanation:[/bold green]")md=Markdown(explanation)console.print(md)else:console.print("\n[bold yellow]Warning: Received non-string data as explanation.[/bold yellow]")console.print(f"Raw data: {str(explanation)}")else:console.print("[bold red]Failed to get explanation from OpenAI.[/bold red]")