o
    gh$                     @   s   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	 d dl
mZmZmZmZmZmZ d dlmZ e  dd	 Zd
d ZdS )    )determine_intent)generate_objective_response)get_action_response)process_request)count_tokens)get_mongo_clientget_character_by_idfetch_mannual_triggersfetch_sectionsfetch_triggersfilter_data_by_character_id)load_dotenvc           )   
   C   sJ  | d}| d}| d}t| }t| }t| }t|}| dd}	| dd}
| dd}td| t||||\}}}d	d
 t|D }dd t|D }dd t|D }dd t|D }td| td| z|dkrt	||}|r| dnd }t| |r||v r|| }|| }t| |d }|| }|| }t
|d |	|
|}t|	 d|
 d| d| d| 	}|||dW S |||d}t| ||}|W S d|v rnd|v rn| d}| d}| d }|d }|d }|d|i |d|i || }|| }t	||d }|r| dnd }t| |rh||d v rk|d | }|| } ||  }|d  }t|||}!|!d! }|!d" }"|!d# }#d|}$t|$ d| d| }||"|#||d$W S W d S W d S || }|| }t	||d }|r| dnd }|r||d v r|d | }%||% }&||& }'t
|'d |	|
|}t|	 d|
 d| d| d| 	}||%|dW S |d rt
|d |	|
|}t|	 d|
 d| d| d| 	}||d |dW S |||d}t| ||}|W S  ty }( z
td%t|( d&d }(~(w ty$ }( z	td't|( d }(~(ww )(Ncharacter_idmessage
section_idnameUnknowngender	backstoryzSections fetched:c                 S      g | ]}|d  qS r    .0triggerr   r   j/var/www/html/aiverses.in/BackEnd/Ai_Agents/services/process_requests/process_request_narriative_design.py
<listcomp>(       z#process_message.<locals>.<listcomp>c                 S   &   i | ]\}}d |v r| d d|qS r   Ngetr   indexsectionr   r   r   
<dictcomp>)      & z#process_message.<locals>.<dictcomp>c                 S      i | ]\}}| d d|qS )r   Nr    r   r#   r   r   r   r   r%   *       c                 S   r'   mannual_trigger_idNr    r(   r   r   r   r%   +   r)   zSections map:ztrigger maps:$Zdecisiondestination_id	objective responser   token_usage)r   r   r   ZActionsZ	Distancesnarrative_design_testactionsobjects	distances	decisionsmappingr+   r1   	action_iddestination)r1   r9   r:   r+   r2   zMissing key: z in sections or triggers.zError processing message: )r!   r
   r   r	   r   printr   list	enumerater   r   r   r   
insert_oner   joinKeyErrorstr	ExceptionRuntimeError))clientdatasidr   r   r   sectionstriggersmannual_triggersr   r   r   mannual_triggertrigger_message_listsection_maptrigger_mapmannual_trigger_mapZintent_resultintentZtrigger_indexr   Zsection_id_newZsection_indexr$   r1   r2   r4   r6   dbZactions_collectionZobjects_collectionr+   mannual_trigger_indexZaction_responseactionr:   Zjoined_actionsnew_section_idnew_section_indexnew_sectioner   r   r   process_message   s   






$


$
$rW   c              
   C   sX  | d}| d}| d}t| }t| }t| }t|}| dd}	| dd}
| dd}dd	 |D }d
d t|D }dd t|D }dd t|D }td|	 td|
 td| td d|v r| d}|| }|| }|d }|| }|| }t|d |	|
|}t|	 d|
 d| d| d| 	}|||dS t	| ||}|S )Nr   r   r   r   r   r   r   c                 S   r   r   r   r   r   r   r   r      r   z%process_request_1.<locals>.<listcomp>c                 S   r   r   r    r"   r   r   r   r%      r&   z%process_request_1.<locals>.<dictcomp>c                 S   s   i | ]	\}}|d  |qS r   r   r(   r   r   r   r%      s    c                 S   r'   r*   r    r(   r   r   r   r%      r)   zname :zgender :zbackstory :zfetching done!!!r+   r-   r.   r/   r0   )
r!   r
   r   r	   r   r=   r;   r   r   rW   )rD   rE   rF   r   r   r   rG   rH   rI   r   r   r   rK   rL   rM   rN   r+   rQ   rJ   rS   rT   rU   r1   r2   r   r   r   process_request_1   sB   






$rX   N)Z1Ai_Agents.services.llm_functions.determine_intentr   Z<Ai_Agents.services.llm_functions.generate_objective_responser   4Ai_Agents.services.llm_functions.get_action_responser   BAi_Agents.services.process_requests.process_request_knowledge_bankr   Z-Ai_Agents.services.llm_functions.count_tokensr   app.database.fetch_datar   r   r	   r
   r   r   dotenvr   rW   rX   r   r   r   r   <module>   s    
 