
    i                    x    d Z ddlmZ ddlZ ej                  dej
                  ej                  z        ZddZy)z
Robust JSON extraction from LLM responses.

Lives in core/ so both brain/ and analysis/ can import it without
violating the V16 layering rule (analysis -> brain forbidden).
    )annotationsNz```(?:json)?\s*(.*?)\s*```c                    | r| j                         sy| j                         }t        j                  |      }|r|j                  d      j                         S |j	                  d      }|j                  d      }|dk7  r||kD  r|||dz    S |S )u  
    Extract a JSON candidate from a Claude / LLM response.

    Strategy:
      1) First fenced block ``` ... ``` or ```json ... ``` anywhere
         in the text (handles prose before/after, case-insensitive).
      2) Fallback: substring from the first '{' to the last '}'
         (rfind, not balanced — sufficient for object-shaped responses).
      3) Last resort: the stripped raw text.

    Empty / whitespace-only input returns "".
        {})strip_JSON_FENCE_REsearchgroupfindrfind)textstrippedmstartends        (/home/work/apex_v16/core/json_parsing.pyextract_json_from_responser      s     tzz|zz|Hh'Awwqz!!MM#E
..
C{sU{cAg&&O    )r   strreturnr   )	__doc__
__future__r   recompileDOTALL
IGNORECASEr   r    r   r   <module>r!      s<    # 	 !IIr   