ó
    ˜!dj  ã                  ó|  • S r SSKJr  SSKrSSKJrJr  SSKJr   " S S\5      r	SS jr
\R                  " S	\R                  \R                  -  5      r\R                  " S
\R                  \R                  -  5      r\R                  " S\R                  \R                  -  5      rSS jrSS jrSS jrSS jrg)a�  Chart selection from an ADE parse response.

`select_charts` takes the raw parse response dict and returns one record per
chart the parse recognized, carrying the page, the grounding box, the
description text, and the reconstructed data table when one came back.

The two API generations mark charts differently. A v1 response carries a
flat `chunks` list; figures are chunks of type `figure`, and a figure is
counted as a chart when our figure prompt made the model emit an HTML data
table inside it. A v2 response marks charts affirmatively: the markdown
wraps each figure in a `<figure type="...">` element, and only elements
typed CHART are selected.
é    )ÚannotationsN)ÚAnyÚOptional)Ú	BaseModelc                  ó`   • \ rS rSr% SrSrS\S'   SrS\S'   SrS	\S
'   Sr	S\S'   Sr
S\S'   Srg)ÚChartRecordé   zOne chart the parse recognized.NzOptional[int]ÚpagezOptional[dict[str, float]]ÚboxÚ ÚstrÚdescriptionzOptional[str]Ú
table_htmlÚ	source_id© )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r
   Ú__annotations__r   r   r   r   Ú__static_attributes__r   ó    Ú</home/mande/repo/quber/src/quber/providers/landing/charts.pyr   r      s<   ‡ Ù)ð €Dˆ-Óð '+€CÐ	#Ó*Ø€K�Óà $€J�Ó$à#€Iˆ}Ö#r   r   c                ó|   • [        U R                  S5      [        5      (       a  [        U S   5      $ [	        U 5      $ )z9Chart records in a parse response, either API generation.Úchunks)Ú
isinstanceÚgetÚlistÚ_from_v1_chunksÚ_from_v2_markdown)Údocs    r   Úselect_chartsr#   &   s2   € ä�#—'‘'˜(Ó#¤T×*Ñ*Ü˜s 8™}Ó-Ð-Ü˜SÓ!Ð!r   z<table\b.*?</table>z2<figure\b[^>]*\btype=\"CHART\"[^>]*>(.*?)</figure>z <description>(.*?)</description>c                óö  • / nU  Hð  nUR                  S5      S:w  a  M  UR                  S5      =(       d    Sn[        R                  U5      nU(       d  MR  UR                  S5      =(       d    0 nUR                  S5      nUR                  [	        Ub  US-   OSUR                  S	5      [        R                  SU5      R                  5       UR                  S
5      UR                  S5      S95        Mò     U$ )zNFigures whose prompted response includes a data table (v1 has no CHART label).ÚtypeÚfigureÚmarkdownr   Ú	groundingr
   Né   r   r   Úid©r
   r   r   r   r   )r   ÚTABLE_REÚsearchÚappendr   ÚsubÚstripÚgroup)r   ÚrecordsÚchunkÚmdÚtabler(   r
   s          r   r    r    2   sÏ   € à!#€GÛˆØ�9‰9�VÓ Ó(ÙØ�Y‰Y�zÓ"×( bˆÜ—‘ Ó#ˆÞÙØ—I‘I˜kÓ*×0¨bˆ	Ø�}‰}˜VÓ$ˆØ�‰ÜØ"&Ñ"2�d˜Q’h¸Ø—M‘M %Ó(Ü$ŸL™L¨¨RÓ0×6Ñ6Ó8Ø Ÿ;™; q›>ØŸ)™) D›/ñö	
ñ ð$ €Nr   c                óÆ  ^• [        U R                  S5      =(       d    0 5      n/ n[        R                  U R                  S5      =(       d    S5       GH  mTR	                  S5      n[
        R                  U5      n[        R                  U5      n[        U4S jU 5       S5      nU=(       d    0 R                  S5      =(       d    0 nUR                  [        UR                  S5      UR                  S	5      U(       a  UR	                  S5      R                  5       OSU(       a  UR	                  S
5      OSU=(       d    0 R                  S5      S95        GM     U$ )a  Figure elements the v2 parse typed CHART in the response markdown.

Page and box come from the structure tree: every figure node's grounding
carries a character range into the markdown, so each CHART element is
matched to the figure node whose range contains its start offset.
Ú	structurer'   r   r)   c              3  ój   >#   • U  H(  n[        UTR                  5       5      (       d  M$  Uv •  M*     g 7f)N)Ú_range_containsÚstart)Ú.0ÚfÚmatchs     €r   Ú	<genexpr>Ú$_from_v2_markdown.<locals>.<genexpr>W   s"   øé € ÐM¢˜1¬?¸1¸e¿k¹k»m×+L—Q‘Q¢ùs   ƒ#3ª	3Nr(   r
   r   r   r*   r+   )Ú_v2_figure_nodesr   ÚV2_FIGURE_REÚfinditerr1   ÚV2_DESCRIPTION_REr-   r,   Únextr.   r   r0   )	r"   Úfiguresr2   ÚbodyÚdescr5   Únoder(   r=   s	           @r   r!   r!   J   s  ø€ ô ˜sŸw™w {Ó3×9°rÓ:€GØ!#€GÜ×&Ñ& s§w¡w¨zÓ':×'@¸b×AˆØ�{‰{˜1‹~ˆÜ ×'Ñ'¨Ó-ˆÜ—‘ Ó%ˆÜÔM¡ÓMÈtÓTˆØ—Z˜R×$Ñ$ [Ó1×7°Rˆ	Ø�‰ÜØ—]‘] 6Ó*Ø—M‘M %Ó(Þ6:˜TŸZ™Z¨›]×0Ñ0Ô2ÀÞ.3˜EŸK™K¨œN¸ØŸ: 2×*Ñ*¨4Ó0ñ÷	
ñ Bð €Nr   c                ó0   ^^• / mSUU4S jjmT" U 5        T$ )z:All figure nodes in the structure tree, in document order.c                ó¤   >• U R                  S5      S:X  a  TR                  U 5        U R                  S5      =(       d    /  H  nT" U5        M     g )Nr%   r&   Úchildren)r   r.   )rH   ÚchildÚfoundÚwalks     €€r   rN   Ú_v2_figure_nodes.<locals>.walki   sB   ø€ Ø�8‰8�FÓ˜xÓ'Ø�L‰L˜ÔØ—X‘X˜jÓ)×/¨RÒ/ˆEÙ�ŽKò 0r   )rH   údict[str, Any]ÚreturnÚNoner   )r7   rM   rN   s    @@r   r@   r@   e   s    ù€ à"$€E÷ð ñ 	ˆ„OØ€Lr   c                óü   • U R                  S5      =(       d    0 R                  S5      =(       d    0 nUR                  S5      UR                  S5      pCUSL=(       a    USL=(       a    X1s=:*  =(       a    U:  $ s  $ )zBTrue when the figure's markdown character range covers ``offset``.r(   Úranger:   ÚendN)r   )r&   ÚoffsetÚrngr:   rU   s        r   r9   r9   s   sg   € à�:‰:�kÓ"×( b×
-Ñ
-¨gÓ
6×
<¸"€CØ—‘˜Ó! 3§7¡7¨5£>ˆ3Ø˜Ð×J ¨D ×J°U×5JÓ5JÀsÑ5JÐJÑ5JÐJr   )r"   rP   rQ   úlist[ChartRecord])r   úlist[dict[str, Any]]rQ   rX   )r7   rP   rQ   rY   )r&   rP   rV   ÚintrQ   Úbool)r   Ú
__future__r   ÚreÚtypingr   r   Úpydanticr   r   r#   ÚcompileÚDOTALLÚ
IGNORECASEr,   rA   rC   r    r!   r@   r9   r   r   r   Ú<module>rc      s¤   ðñõ #ã 	ß  å ô$�)ô $ô"ð �:Š:Ð,¨b¯i©i¸"¿-¹-Ñ.GÓH€Ø�zŠzÐOÐQS×QZÑQZÐ]_×]jÑ]jÑQjÓk€Ø—J’JÐBÀBÇIÁIÐPR×P]ÑP]ÑD]Ó^Ð ôô0ô6õKr   