Ë
    õ¡Ÿj�  ã                  óª   — d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
 ddlmZ  ej                  e«      ZdZdd„Zddd	œ	 	 	 	 	 	 	 	 	 	 	 	 	 dd
„Zy)z‘
author: Yagnik Poshiya
organization: Webbrains Technologies Private Limited

Dedicated Mongo query generation LLM for live MongoDB connections.
é    )ÚannotationsN)ÚSettings)ÚGenerationConfigÚ	LLMClientÚLLMUsage)Úbuild_llm_clientak  You are a specialized MongoDB query generator.
Your ONLY job is to translate a visitor's natural-language question into a
single safe MongoDB query spec JSON object.

OUTPUT FORMAT (strict JSON object only):
{
  "operation": "find" | "aggregate",
  "collection": "<collection_name>",
  "filter": { ... },                  // for find
  "projection": { ... },              // optional for find
  "sort": [["field","asc"|"desc"]],   // optional for find
  "limit": 1..50,                     // optional for find (default 50)
  "pipeline": [ ... ]                 // for aggregate
}

RULES:
1. Output MUST be valid JSON with exactly one query spec object.
2. Use ONLY collections and fields provided in schema context.
3. Never use write/update/delete/admin operations.
4. Allowed operations are only "find" and "aggregate".
5. For count-like questions, prefer aggregate with $match + $count.
6. Add a limit <= 50 when returning documents.
7. Do not use JavaScript execution operators like $where.
8. Return {"operation":"", "reason":"..."} only when schema lacks required data.
9. Do NOT output markdown fences or extra commentary.
c                óB   — t        | j                  | j                  «      S )N)r   Úsql_gen_providerÚsql_gen_model)Úsettingss    úM/var/www/html/agent-engine/apps/agents/rag-agent/src/agent/mongo_generator.pyÚ_build_mongo_gen_clientr   0   s   € ä˜H×5Ñ5°x×7MÑ7MÓNÐNó    )Úprevious_specÚprevious_errorc               ó  — t        | «      }|r|rd|› d|› d|› d|› d|› d�}nd|› d|› d|› �}t        › d|› �}|j                  |t        dd	¬
«      ¬«      }	|	j                  j                  «       }
|
si d|	j                  fS 	 t        j                  |
«      }t        |t        «      si |
|	j                  fS ||
|	j                  fS # t        j                  $ r+ t        j                  d|
dd «       i |
|	j                  fcY S w xY w)z/Return (query_spec_dict, raw_json_text, usage).z
Database: z


Schema:
z

Visitor's question:
z

Previous query spec:
z

Problem with previous spec:
z(

Generate a corrected Mongo query spec.z

g333333Ã?T)ÚtemperatureÚresponse_json)ÚconfigÚ z&mongo_generator: non-JSON response: %sNéÈ   )r   Ú_SYSTEM_INSTRUCTION_MONGOÚgenerater   ÚtextÚstripÚusageÚjsonÚloadsÚJSONDecodeErrorÚloggerÚwarningÚ
isinstanceÚdict)r   Úuser_messageÚschema_detailÚdb_labelr   r   ÚclientÚ
user_blockÚpromptÚrespr   Údatas               r   Úgenerate_mongo_queryr,   5   sI  € ô % XÓ.€Fá™à˜˜
ð #Ø%�ð '$Ø$0 >ð 2%Ø%2 Oð 4,Ø,:Ð+;ð <5ð	5ñ 	ð ˜˜
ð #Ø%�ð '$Ø$0 >ð3ð 	ô *Ð*¨$¨z¨lÐ;€FØ�?‰?˜6Ô*:ÀtÐ[_Ô*`ˆ?Óa€DØ�9‰9�?‰?Ó€DÙØ�2�t—z‘zÐ!Ð!ð$Ü�z‰z˜$Óˆô �dœDÔ!Ø�4˜Ÿ™Ð#Ð#Ø��t—z‘zÐ!Ð!øô ×Ñò $Ü�‰Ð?ÀÀdÀsÀÔLØ�4˜Ÿ™Ð#Ò#ð$ús   ÂC Ã;DÄD)r   r   Úreturnr   )r   r   r$   Ústrr%   r.   r&   r.   r   ú
str | Noner   r/   r-   ztuple[dict, str, LLMUsage])Ú__doc__Ú
__future__r   r   ÚloggingÚconfig.settingsr   Úllm.baser   r   r   Úllm.factoryr   Ú	getLoggerÚ__name__r    r   r   r,   © r   r   ú<module>r9      s’   ðñõ #ã Û å $ß :Ñ :Ý (à	ˆ×	Ñ	˜8Ó	$€ðÐ ó:Oð !%Ø!%ñ("Øð("ð ð("ð ð	("ð
 ð("ð ð("ð ð("ð  ô("r   