
    |j3                        d dl mZ d dlmZmZmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZmZmZmZ d d	lmZmZmZ  G d
 de      Z G d de      Z G d dee      Z G d dee      Z G d dee      Z G d dee      Z  G d de      Z! G d de      Z" G d de      Z# G d dee      Z$ G d de      Z% G d  d!e      Z&g d"d"fd#e'd$ed"z  d%ed"z  d&efd'Z(g d"fd(ed)ed#e'd*ed&e)f
d+Z* G d, d-      Z+y").    )Enum)AnyLiteralOptional)	BaseModelField)settings)retrieve)
ChatOpenAI)parse_partial_json)ChatPromptTemplateHumanMessagePromptTemplateSystemMessagePromptTemplateAIMessagePromptTemplate)BASE_SYSTEM_TEMPLATEget_hotel_details SYSTEM_TEMPLATE_FOR_CONTEXT_DATAc                   0    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zy
)ToolEnummodify_reservation_toolsearch_room_tooladd_to_cartadd_billing_detailssend_email_toolclear_cart_toolnote_special_requestget_reservation_detailsno_toolN)__name__
__module____qualname__modifyReservationsearchbookingbilling
send_email
clear_cartspecial_requestr   default     4/var/www/html/ls-ibe-bot-apis/app/agents/__init__.pyr   r      s1    1FG#G"J"J,O7Gr+   r   c                   (   e Zd ZU  ed      Zeed<    ed      Zeed<    eddd	      Ze	ed
<    eddd	      Z
e	ed<    eg d      Zee   ed<    eg d      Zee   ed<    edd      Zeed<    edd      Zeed<    edd      Zeed<   y)AgentParamszCheck-in date in MM-DD-YYYY format. Always populate this field. Use the user's check-in date if provided; otherwise use the default CHECK-IN value from the prompt.descriptioncheck_inzCheck-out date in MM-DD-YYYY format. Always populate this field. Use the user's check-out date if provided; otherwise use the default CHECK-OUT value from the prompt.	check_out      zNumber of adults for the room search or booking. Always populate this field. Use the user's value if provided; otherwise default to 2.)r)   ger0   
adultCountr   zNumber of children for the room search or booking. Always populate this field. Use the user's value if provided; otherwise default to 0.
childCountz9Requested room names if the user mentions one explicitly.r)   r0   	room_namezList of exact room_type_ids for booking or add-to-cart requests. Use only when the requested rooms is known from context or prior results. Never guess or fabricate this value.room_type_idNa  Reservation confirmation number for cancellation-email requests. Itenrary is also accepted here. Reservation confirmation number for special request notes if reservation is already confirmed. Itenrary is also accepted here.Reservation confirmation number for getting reservation details when calling get_reservation_details tool. Itenrary is also accepted here.Use a value from the user query or conversation history when available.confirmation_numberzEmail address of the user for special request notes. Email address of the user for getting reservation details when calling get_reservation_details tool. Use a value from the user query or conversation history when available.emailzSpecial request notes from the user for the reservation. Don't copy exact text from user query, Explain the request in your own words and summarize it in your own words.r(   )r   r    r!   r   r1   str__annotations__r2   r6   intr7   r9   listr:   r;   r<   r(   r*   r+   r,   r.   r.      s   NHc  PIs  d	J  d	J  !OItCy  $3L$s)   %V   VE3  !OS r+   r.   c                       e Zd ZdZdZy)SortDirection	ascending
descendingN)r   r    r!   rC   rD   r*   r+   r,   rB   rB   Y   s    IJr+   rB   c                       e Zd ZdZdZy)	SortFieldrate	occupancyNr   r    r!   rG   rH   r*   r+   r,   rF   rF   ]       DIr+   rF   c                       e Zd ZdZdZy)FilterFieldrG   rH   NrI   r*   r+   r,   rL   rL   a   rJ   r+   rL   c                   $    e Zd ZdZdZdZdZdZdZy)FilterOperatorz==z!=><z>=z<=N)	r   r    r!   eqnegtltgtelter*   r+   r,   rN   rN   e   s     	B	B	B	B
C
Cr+   rN   c                   b    e Zd ZU  ed      Zeed<    ed      Zeed<    ed      Z	e
ed<   y)	FilterConditionz/Field name to filter on (e.g., rate, occupancy)r/   fieldzComparison operatoroperatorzValue to compare againstvalueN)r   r    r!   r   rY   rL   r>   rZ   rN   r[   r   r*   r+   r,   rX   rX   m   s2    +\]E;]$1FGHnG#=>E3>r+   rX   c                   F    e Zd ZU  ed      Zeed<    ed      Zeed<   y)
SortConfigzField to sort byr/   rY   zSort direction	directionN)	r   r    r!   r   rY   rF   r>   r^   rB   r*   r+   r,   r]   r]   s   s"    );<E9<$1ABI}Br+   r]   c                   V    e Zd ZU  edd      Zedz  ed<    eed      Zee	   ed<   y)FiltersNzSorting configurationr8   sortzList of filter conditionsdefault_factoryr0   
conditions)
r   r    r!   r   ra   r]   r>   r@   rd   rX   r*   r+   r,   r`   r`   w   s<    #+D*t
  )./)J_% r+   r`   c                        e Zd ZdZdZdZdZdZy)BillingFormStepaddons	insurancecustomer_details_formpromo_code_formpayment_details_formN)r   r    r!   rg   rh   ri   rj   rk   r*   r+   r,   rf   rf      s    FI3'O1r+   rf   c                   0   e Zd ZU  ed      Zeed<    edd      Zee	   ed<    ed      Z
eed	<    ed
      Zeed<    edd      Zeed<    edd      Zeed<    edd       Zeed      ed<    eed      Zee   ed<    edd      Zeed<   y)AgentSchemaa  Tool to execute for the user's request. Use add_to_cart for booking or add-to-cart actions, search_room_tool for room search or reopening the room slider, send_email_tool for cancellation email requests, note_special_request for special request notes, and no_tool when only a conversational answer is needed. The no_tool path may still include room_type_ids so the frontend can open the slider from general room matches.r/   toolNzMif calling the billing tool, the step of the billing form to show to the userr8   billing_form_stepzvFriendly user-facing response. Keep it positive and concise. If a tool will be used, start naturally with 'Let me...'.answerz8Structured parameters extracted from the user's request.paramsz#Filters to apply to the room searchfilterszVNumber of rooms to return in the frontend slider. Use -1 to return all matching rooms.
room_counta  Ordered list of hotel IDs whose rooms should be returned.

Rules:
1. Default to ['124'] and show Bayview Plaza rooms first.
2. If the user asks for more options or other hotels, use ['123', '124', '125'].
3. If the user explicitly requests a specific hotel, include only the requested hotel IDs.
4. Do not change hotels unless the user asks or signals dissatisfaction.
5. room_type_ids must stay scoped to these hotel_ids.c                      dgS )N124r*   r*   r+   r,   <lambda>zAgentSchema.<lambda>   s    wr+   )r0   rc   )123rv   125	hotel_idsa  Specific room_type_id values to render on the frontend when search_room_tool is not being used. If the answer lists or recommends room types from GENERAL-ROOM-DETAILS, always include the matching room_type_ids here. This is required for frontend slider rendering. Only include room_type_ids from the currently selected hotel_ids. By default that means only hotel 124 unless the user explicitly asks for another hotel or broader hotel options. Leave empty only when no specific room types are being referenced.rb   room_type_idsr   a  Total number of allowed pets (dogs and cats) in the room. Use 0 for no pets. Always include this field if the user explicitly asks for pets or mentions they are bringing pets. Frontend will use this field to automatically add pets add-on to the cart if user add a room to cart in the same chat. Wait for clarification from the user if you are not sure about the number of pets. If user mention any pet so in every conversation you have keep track of the number of pets and update this field accordingly becasue this is the only way to know the number of pets in the room until chat is over. Make it back to 0 if user mention no pets or no more pets or switch to hotel which doesn't allow pets.
total_pets)r   r    r!   r   rn   r   r>   ro   r   rf   rp   r=   rq   r.   rr   r`   rt   r?   rz   r@   r   r{   r|   r*   r+   r,   rm   rm      s    ~
D( 
 49c4x0  HFC   NFK  9GW  lJ  5:	@ '5(ItG/01 (  %Q
 M49 
  LJ r+   rm   c                   J    e Zd ZU  edd      Zeed<    edd      Zeed<   y)	ContextDatazdThe standalone query to retrieve the most relevant context for answering the user's latest question. )r0   r)   queryzIWhether to skip vector database retrieval for the user's latest question.FskipN)	r   r    r!   r   r   r=   r>   r   boolr*   r+   r,   r~   r~      s5      $J  TV  WE3  W#nx}~D$~r+   r~   Nhistorytool_response	tool_callreturnc                 H   t         st        d      t        j                  t               g| t	        j                  d      }|rJ|rH|j                  t        j                  d             |j                  t        j                  d             t        j                  |      S )NzInvalid BASE_SYSTEM_TEMPLATEz
{question}z{tool_call}zTool response: {tool_response})	r   
ValueErrorr   from_templater   appendr   r   from_messages)r   r   r   messagess       r,   get_chat_promptr      s    788 	$112FG	 	#00>H
 /==mLM/==>^_`++H55r+   questionsub_hotel_id	callbacksc                 j  K   t        t        j                  d      }t        j                  t
              g}t        j                  |      |j                  t              z  }|j                  || dd|d       d {   }|j                  ryt        |j                  |       d {   S 7 /7 w)Nzgpt-5.4-mini)api_keymodel)r   r   zContext Data)run_namer   )configr   )r   r	   OPENAI_API_KEYr   r   r   r   r   with_structured_outputr~   ainvoker   r
   r   )r   r   r   r   llmr   chainresults           r,   get_context_datar      s     
X44N
KC 	$112RSH ,,X6#:T:TU`:aaE %'x.Xn|  LU  bV  !W  WF{{&,,555 W
 6s$   A=B3?B/ *B3*B1+B31B3c                   z    e Zd Zdej                  dfdededededz  fdZi g dddfded	ed
edz  dedz  de	f
dZ
y)
HotelAgentzgpt-5.1Nr   r   r   conversation_idc                 J    || _         || _        || _        || _        d | _        y )N)r   r   r   r   r   )selfr   r   r   r   s        r,   __init__zHotelAgent.__init__   s'    (
+:*.r+   inputr   r   r   r   c                  K   t        | j                  | j                  d      }t        j	                         }|d   d   d   j                  dd| j                   d      |d   d   d<   |d   d	   d   j                  d
| j                        |d   d	   d<   |j                  |      }t        |j                  d      | j                  ||       d {   }	|	|d<   t        |||      |z  }
|r|r
||d<   ||d<   |
j                  |d| j                  r| j                  nd| j                  r| j                  ndg|d      }d}d}|2 3 d {   }|r|j                  d      dk(  r|j                  d      r|j                  d      j                  d      rt        |j                  d      j                  d      d      rf|j                  d      j                  d      j                  }|s||z  }t        |      }|%d|v r!|d   t!        |      d  }||z  }|dk(  r| |}|s|j                  d      dk(  s|j                  di       j                  d      }|t        j#                  |      | _        97 7 86 y w)NT)r   r   	streaming
propertiesrz   r0   z['124']z['z']r{   rv   r   hotel_context)r   r   r   r   r   z
Main Agentzunknown-conversationzunknown-hotel)r   tagsr   )r   r   r   eventon_chat_model_streamdatachunkcontentrp   on_chain_endoutput)r   r   r   rm   model_json_schemareplacer   r   r   getr   astream_eventsr   hasattrr   r   lenmodel_validater   )r   r   r   r   r   r   r   schemallm_with_schemacontextr   stream_resultfinal_answerrp   r   tokenescaped_text	new_tokenr   s                      r,   streamzHotelAgent.stream   s    TZZ4P..0;A,;OP[;\]j;k;s;st}  BD  EI  EV  EV  DW  WY  @Z  <[|[)-8?El?STc?der?s?{?{  }B  DH  DU  DU  @V|_-m<44V<(:)>@Q@QSZ\eff!(o}XabettY%2E/"!*E+,,(151E1E--Kax|  yJ  yJcgctct  P_  `& - 
 ( 	E%7+/EE99V$6):)>)>w)Guyy044W=yI %		& 1 5 5g > F F$$$-'9,'G'3L8P(4X(>s6{|(LI"i/F(B ("+O!'7+~=62.228<%"-"<"<V"DDKS g(	E=sJ   CJJA7JJJJCJ/J#J)JJJ)r   r    r!   r	   r   r=   r   dictr@   r   r   r*   r+   r,   r   r      s    7@QYQhQh  IM /S / /3 /{~  BF  |F / *,R]a{  SW 1E$ 1Ed 1EPSVZPZ 1Enqtxnx 1E  MP 1Er+   r   ),enumr   typingr   r   r   pydanticr   r   app.configs.settingsr	   app.services.pineconer
   !langchain_openai.chat_models.baser   "langchain_core.output_parsers.jsonr   langchain_core.prompts.chatr   r   r   r   app.utils.constantsr   r   r   r   r.   r=   rB   rF   rL   rN   rX   r]   r`   rf   rm   r~   r@   r   r   r   r   r*   r+   r,   <module>r      sH    ) ) % ) * 8 A M  M i i	t 	@) @DC T #t S$ ?i ?C Ci 2c4 28) 8t)  $&4ae 6D 6cDj 6TWZ^T^ 6j| 6 NPbf 6S 6 6d 6\_ 6ko 6;E ;Er+   