3737LOCAL_MARKDOWN_LINK_RE = re .compile (r"\[([^\]]+)\]\((/[^)\n]+)\)" )
3838HANDOFF_SUMMARY_RE = re .compile (r"^(?:#{1,6}\s*)?\*{0,2}Handoff Summary\*{0,2}\s*(?:\n|$)" , re .IGNORECASE )
3939CODEX_GIT_DIRECTIVE_RE = re .compile (r"(?m)^[ \t]*::git-[A-Za-z0-9_-]+\{[^\n]*\}[ \t]*\n?" )
40+ CODEX_USER_FILE_MENTIONS_RE = re .compile (
41+ r"\A\s*#{0,6}\s*Files mentioned by the user:\s*\n.*?^\s*#{0,6}\s*My request for Codex:\s*\n?" ,
42+ re .DOTALL | re .MULTILINE ,
43+ )
4044SUPPORTED_IMAGE_MEDIA_TYPES = {"image/png" , "image/jpeg" , "image/gif" , "image/webp" }
4145COMPACTION_PLACEHOLDER = (
4246 "[Context compacted - earlier conversation summarized to continue past the context window]"
@@ -355,6 +359,8 @@ def normalize_content_blocks(
355359 data_image_uploader : DataImageUploader | None = None ,
356360) -> Any | None :
357361 if isinstance (raw_content , str ):
362+ if role == "user" :
363+ raw_content = clean_codex_user_text (raw_content )
358364 return normalize_text_with_images (raw_content , image_uploader )
359365 if not isinstance (raw_content , list ):
360366 if raw_content is None :
@@ -368,6 +374,8 @@ def normalize_content_blocks(
368374 block_type = str (block .get ("type" ) or "" )
369375 text = block .get ("text" )
370376 if block_type in {"input_text" , "output_text" , "text" } and isinstance (text , str ):
377+ if role == "user" :
378+ text = clean_codex_user_text (text )
371379 append_blocks (blocks , normalize_text_blocks (text , image_uploader ))
372380 continue
373381 if block_type == "input_image" :
@@ -397,7 +405,7 @@ def normalize_content_blocks(
397405
398406
399407def normalize_user_event_content (message : str , local_images : Any , image_uploader : ImageUploader | None = None ) -> Any | None :
400- blocks = normalize_text_blocks (message , image_uploader )
408+ blocks = normalize_text_blocks (clean_codex_user_text ( message ) , image_uploader )
401409 if isinstance (local_images , list ):
402410 for image_path in local_images :
403411 if not isinstance (image_path , str ):
@@ -575,6 +583,10 @@ def clean_codex_text(text: str) -> str:
575583 return re .sub (r"\n{3,}" , "\n \n " , cleaned )
576584
577585
586+ def clean_codex_user_text (text : str ) -> str :
587+ return clean_codex_text (CODEX_USER_FILE_MENTIONS_RE .sub ("" , text , count = 1 ))
588+
589+
578590def file_url_local_markdown_link_targets (text : str ) -> str :
579591 def replace (match : re .Match [str ]) -> str :
580592 label = match .group (1 ).strip ()
0 commit comments