Ë
    £Ìjz"  ã                  óÎ  — U d Z ddlmZ ddlZddlmZ ed   ZdZdZdZ	d	Z
i  ee	«      D � �ci c]  \  } } e|«       e| «      “Œ c}} ¥ ee
«      D � �ci c]  \  } } e|«       e| «      “Œ c}} ¥Zd
d
dddd
d
ddddddœZded<   ddddœZded<   d#d„Zd$d„Zd%d„Zd&d„Zd&d„Zd&d„Z eh d£«      Z eh d£«      Z eh d£«      ZdZd'd„Zd$d „Zd&d!„Zd(d"„Z yc c}} w c c}} w ))uÁ  
author: Yagnik Poshiya
organization: Webbrains Technologies Private Limited

Language handling for visitor messages.

Scope is deliberately narrow: Gujarati, Hindi and English, which are the
languages the product supports. Everything here is deterministic string work â€”
no model call â€” because it runs on the blocking path of every turn and the
latency budget in docs/architecture/rag-agent-latency-playbook.md leaves no
room for an extra round trip just to find out what language someone typed in.

Detection is by script, which is exact for Gujarati and Devanagari and useless
for anything written in Latin letters. That limit is the point: "mane 30 lakh
nu ghar joiye" is Gujarati in Latin script and no range check will ever see it.
So detection is used to *strengthen* a prompt instruction when it is certain,
never to overrule the model, and the instruction itself always tells the model
to mirror the visitor regardless of what detection concluded.
é    )ÚannotationsN)ÚLiteral)ÚguÚhiÚenÚunknown)i€
  iÿ
  )i 	  i	  u   à«¦à«§à«¨à«©à«ªà««à«¬à«­à«®à«¯u   à¥¦à¥§à¥¨à¥©à¥ªà¥«à¥¬à¥­à¥®à¥¯ÚlakhÚcroreÚthousand)u	   àª²àª¾àª–u   àª²àª¾àª–à«‹u   àª•àª°à«‹àª¡u   àª•àª°à«‹àª¡à«‹u   àª¹àªœàª¾àª°u	   à¤²à¤¾à¤–u   à¤²à¤¾à¤–à¥‹à¤‚u   à¤•à¤°à¥‹à¤¡à¤¼u   à¤•à¤°à¥‹à¤¡u   à¤•à¤°à¥‹à¤¡à¤¼à¥‹à¤‚u   à¤¹à¤œà¤¾à¤°u   à¤¹à¤œà¤¼à¤¾à¤°zdict[str, str]Ú_SCALE_WORDSÚGujaratiÚHindiÚEnglish)r   r   r   Ú_LANGUAGE_NAMESc                ó:   ‡‡— |\  ŠŠt        ˆˆfd„| D «       «      S )Nc              3  óR   •K  — | ]  }‰t        |«      cxk  r‰k  sŒn nd –— Œ  y­w)é   N)Úord)Ú.0ÚchÚhighÚlows     €€úF/var/www/html/agent-engine/apps/agents/rag-agent/src/agent/language.pyú	<genexpr>z"_count_in_range.<locals>.<genexpr>I   s!   øè ø€ Ò:�R 3¬#¨b«'Ô#9°TÖ#9ŒqÑ:ùs   ƒ'�
')Úsum)ÚtextÚblockr   r   s     @@r   Ú_count_in_ranger   G   s   ù€ Ø�I€CˆÜÔ:˜4Ô:Ó:Ð:ó    c                óž   — t        | xs d«      }|j                  «       syt        |t        «      }t        |t        «      }|s|r	||k\  rdS dS y)a‹  
    Best-effort language of a visitor message, by script.

    Returns "unknown" for Latin-script input rather than guessing "en": a
    romanised Gujarati or Hindi message is Latin script, and claiming English
    would make the prompt instruct the model to answer in the wrong language.
    "unknown" lets the caller fall back to "mirror the visitor", which is
    correct in every case.
    Ú r   r   r   )ÚstrÚstripr   Ú	_GUJARATIÚ_DEVANAGARI)r   ÚvalueÚgujaratiÚ
devanagaris       r   Údetect_languager)   L   sO   € ô �’
˜‹O€EØ�;‰;Œ=Øä˜u¤iÓ0€HÜ  ¬Ó4€Já‘:Ø :Ò-ˆtÐ7°4Ð7Ør   c                ó,   — t         j                  | «      S )N)r   Úget)Úcodes    r   Úlanguage_display_namer-   b   s   € Ü×Ñ˜tÓ$Ð$r   c                óF   — t        | xs d«      j                  t        «      S )zNRewrite Gujarati and Devanagari digits to ASCII. Leaves everything else alone.r!   )r"   Ú	translateÚ
_DIGIT_MAP©r   s    r   Únormalize_indic_digitsr2   f   s   € äˆtŠz�r‹?×$Ñ$¤ZÓ0Ð0r   c                ó¼   — t        | xs d«      }t        j                  «       D ]   \  }}||v sŒ|j                  |d|› d�«      }Œ" t	        j
                  dd|«      S )zERewrite Indic scale words to the English words the numeric rules use.r!   ú z	[ \t]{2,})r"   r   ÚitemsÚreplaceÚreÚsub)r   r&   ÚsourceÚtargets       r   Únormalize_scale_wordsr;   k   s_   € ä�’
˜‹O€EÜ&×,Ñ,Ó.ò 9‰ˆ�Ø�UŠ?Ø—M‘M &¨A¨f¨X°Q¨-Ó8‰Eð9ô �6‰6�,  UÓ+Ð+r   c                óF   — t        t        | «      «      j                  «       S )zû
    Prepare a message for parameter extraction.

    Only numerals and scale words are touched. The rest of the sentence stays
    in its original language, because the extractor still needs it to work out
    which parameter a value belongs to.
    )r;   r2   r#   r1   s    r   Ú!normalize_numerics_for_extractionr=   t   s   € ô !Ô!7¸Ó!=Ó>×DÑDÓFÐFr   >   ÚcheÚshuÚaapoÚchheÚmaneÚmariÚmaruÚjoiyeÚketlaÚketliÚkyareÚnathiÚtamneÚbatavoÚtamaraÚtamaru>   ÚaapÚhaiÚhainÚhogaÚmeraÚmeriÚnahiÚbataoÚkaiseÚkitnaÚkitneÚmujheÚnahinÚdikhaoÚkripyaÚchahiye>   ÚkyaÚachaÚbhaiÚgharr	   r
   Úthodaé   c           	     óv   — t        t        j                  dt        | xs d«      j	                  «       «      «      S )Nz[a-z]+r!   )Úsetr7   Úfindallr"   Úlowerr1   s    r   Ú_roman_tokensrh   š   s)   € ÜŒr�z‰z˜)¤S¨ª°£_×%:Ñ%:Ó%<Ó=Ó>Ð>r   c                ó  — t        | «      }|sy|t        z  }|t        z  }|t        z  }t	        |«      t	        |«      z   t	        |«      z   t
        k  ryt	        |«      t	        |«      kD  ryt	        |«      t	        |«      kD  ryy)aH  
    Best-effort language for Indic text written in Latin letters.

    Returns "unknown" unless at least two distinct marker words are present,
    and unless one language clearly outscores the other. Guessing wrong here
    means answering an English visitor in Hindi, which is far worse than
    leaving them in English.
    r   r   r   )rh   Ú_GUJARATI_ROMANÚ_HINDI_ROMANÚ_SHARED_ROMANÚlenÚ_ROMAN_MIN_MARKERS)r   Útokensr   r   Úshareds        r   Údetect_romanised_languagerq   ž   s}   € ô ˜4Ó €FÙØà	”/Ñ	!€BØ	”,Ñ	€BØ”mÑ#€Fä
ˆ2ƒw”�R“Ñœ3˜v›;Ñ&Ô);Ò;ØÜ
ˆ2ƒw”�R“ÒØÜ
ˆ2ƒw”�R“ÒØØr   c                óˆ   — t        t        | «      «      }|r	d|› d|› d�S t        t        | «      «      }|rd|› d|› d|› d�S 	 y)	u´  
    Prompt fragment telling the model which language to answer in.

    Three cases, in order of confidence. Native script is conclusive, so name
    the language and the script. Romanised Indic is a vocabulary guess, so name
    the language but insist on Latin script â€” someone who typed "mane ghar
    joiye" wants an answer they can read back, not Gujarati script. Otherwise
    fall back to mirroring, which is never wrong.
    z- The visitor wrote in z. Reply entirely in z›, including any labels, headings or list items. Do not switch to English partway through, and do not translate proper nouns such as place or project names.z- The visitor wrote z+ using Latin letters (romanised). Reply in u?    but keep the SAME Latin script they used â€” do not switch to z2 script. Do not switch to English partway through.zÈ- Reply in the same language the visitor used, including romanised Gujarati or Hindi written in Latin letters. Match their script as well as their language, and do not switch language partway through.)r-   r)   rq   )r   ÚnameÚ	romaniseds      r   Úanswer_language_instructionru   ¸   s{   € ô !¤°Ó!6Ó7€DÙà% d VÐ+?À¸vð FYð Zð	
ô &Ô&?ÀÓ&EÓF€IÙà" 9 +Ð-XØˆkÐXØˆkÐKðMð	
ð	Iðr   c                  ó   — 	 y)a  
    Prompt fragment for parameter extraction.

    The knowledge base and the customer databases are English. A value lifted
    verbatim out of a Gujarati sentence goes into a SQL WHERE clause and matches
    nothing, so the extractor has to hand back the English form.
    u`  - Stored data is in English. Return parameter VALUES in English even when the visitor wrote in another language or script: transliterate place and proper names to their usual English spelling (àªµàª¡à«‹àª¦àª°àª¾ -> Vadodara, à¤¸à¥‚à¤°à¤¤ -> Surat) and return numbers as digits. Parameter NAMES are already English and must be returned exactly as given.© rw   r   r   Údatabase_value_language_rulerx   Ù   s   € ð	9ðr   )r   r"   r   ztuple[int, int]ÚreturnÚint)r   r"   ry   ÚLanguageCode)r,   r{   ry   z
str | None)r   r"   ry   r"   )r   r"   ry   zset[str])ry   r"   )!Ú__doc__Ú
__future__r   r7   Útypingr   r{   r$   r%   Ú_GUJARATI_DIGITSÚ_DEVANAGARI_DIGITSÚ	enumerater   r"   r0   r   Ú__annotations__r   r   r)   r-   r2   r;   r=   Ú	frozensetrj   rk   rl   rn   rh   rq   ru   rx   )ÚiÚds   00r   ú<module>r†      sT  ðòõ( #ã 	Ý àÐ2Ñ3€ð €	Ø€ð
 4Ð Ø5Ð ðÙ"+Ð,<Ó"=×>™$˜!˜Q�sˆ1ƒv‰s�1‹v�~Ó>ðá"+Ð,>Ó"?×@™$˜!˜Q�sˆ1ƒv‰s�1‹v�~Ó@ð€
ð ØØØØàØØØØ$ØØ!ñ €ˆnó ð$ Ø
Ø
ñ#€�ó ó;ó
ó,%ó1ó
,óGñ* ò ó €ñ ò ó €ñ ÒSÓT€ð Ð ó?óó4ôBùóe ?ùÛ@s   ­CÁC!