o
    h                     @   s   d dl mZ d dl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 e Ze
 Ze Zd dlZd d	lmZ G d
d dZdS )    )HTTPExceptionN   )HashGenerator)UserJourney)MicrolearningDurationEstimator)Synthesizer)MicrolearningTopicValidator)DefaultEstimatorc                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )MicrolearningServicec                 C   s~   t  | _|| _t|| _d| j d| _d| j d| _d| j d| _d| j d| _	d| j d| _
d| j d| _d S )	Nz	research/z.mdzoutput/zoutput1/zoutput2/zparsed_course_content/z.jsonz	defaults/)r   crew_instance
input_datar   generate_input_hash
input_hashresearch_fileoutput_fileoutput_file_1output_file_2json_output_pathdefaults_dir)selfr    r   /home/azureuser/microlearn/backend/user_journey_with_openai/agentic_workflow/src/user_journey_service/services/user_journey_service.py__init__   s   zMicrolearningService.__init__c              
   C   s   zP|   r/| | j\}}|rdd|| jdW S |  r!td n
td dddW S |   |  }|  \}}| 	||| | | j\}}dd	|| jdW S  t
ye } z	td
d| dd }~ww )NsuccesszFetched existing result.)statusmessageoutputhashidzNeed all the Agents..zInvalid topic...failedzInvalid topic.)r   r   zCrew execution completed.i  zError running crew: )status_codedetail)_is_first_iteration_fetch_existing_resultr   r   _validate_the_topicprint_execute_first_iteration_estimate_duration_load_content_based_feedback_execute_restructure_crew	Exceptionr   )r   existsresultavailable_timecontent1content2er   r   r   run_journey,   s&   
z MicrolearningService.run_journeyc                 C   sH   t | j}td|  |  }|drtd dS td dS )NzThe validator output is : zvalid topiczThis is a valid topic.TzThis is an invalid topic.F)topic_validatorvalidate_topicr   r$   striplower
startswith)r   validate
normalizedr   r   r   r#   D   s   
z(MicrolearningService._validate_the_topicc                 C   s   d}t ||t jS )Nz&### Q\d+:\s*\n?(.*?)(?=\n### Q\d+:|\Z))refindallDOTALL)r   md_textpatternr   r   r   parse_questionsP   s   z$MicrolearningService.parse_questionsc                 C   s&   t d| jj  | jjdkrdS dS )NzInside feedback test : zFirst iterationTF)r$   r   feedback)r   r   r   r   r!   T   s   z(MicrolearningService._is_first_iterationc                 C   sP   t j|r&t|d}| }W d    d|fS 1 sw   Y  d|fS dS )NrT)F )ospathr*   openread)r   	file_pathfiler+   r   r   r   r"   Z   s   

z+MicrolearningService._fetch_existing_resultc                 C   sJ   |  | j\}}|rtd d S | j }| jj| jd}|j|d d S )Nzresearcher already exists..)r   inputs)r"   r   r$   r   dictr   researcher_crewkickoff)r   r*   _rH   crewr   r   r   r%   b   s   
z-MicrolearningService._execute_first_iterationc                 C   s>   t | j}t|tr|d}t|d }td|  |S )Nz[]z minutesz"The available time estimated is : )duration_estimatorestimate_durationr   
isinstancestrr3   r$   )r   r,   r   r   r   r&   k   s   

z'MicrolearningService._estimate_durationc                 C   sH   |   r| | j\}}||fS | | j\}}| | j\}}||fS )N)r!   r"   r   r   r   )r   rL   contentr-   r.   r   r   r   r'   s   s   z1MicrolearningService._load_content_based_feedbackc                 C   sn   t | j}| }t|d  | j }||d< ||d< ||d  | jj| j|| jj	d}|j
|d d S )NdefaultsrR   r,   )r   r,   r>   rG   )r	   r   runr$   rI   updater   restructure_crew1r   r>   rK   )r   r,   r-   r.   default_estimatoruser_defaultsrH   rM   r   r   r   r(   |   s   

z.MicrolearningService._execute_restructure_crewc                 C   s   |  | j\}}|  | j\}}tj| jj| jj| jj||d}t	j
t	j| jdd t| jddd}|| W d    d S 1 sEw   Y  d S )N)designationskills
experiencer-   r.   T)exist_okwzutf-8)encoding)r"   r   r   synthesizerreview_and_combiner   	Job_TitleSkills
ExperiencerA   makedirsrB   dirnamer   rC   write)r   rL   user_journey1user_journey2combined_dataout_filer   r   r   _combine_user_journeys   s    "z+MicrolearningService._combine_user_journeysN)__name__
__module____qualname__r   r0   r#   r=   r!   r"   r%   r&   r'   r(   rk   r   r   r   r   r
      s    		r
   )fastapir   rA   utils.hashingr   user_journey_service.crewr   2user_journey_service.processors.duration_estimatorr   8user_journey_service.processors.user_journey_synthesizerr   /user_journey_service.processors.topic_validatorr   rN   r_   r1   r8   .user_journey_service.services.defaults_servicer	   r
   r   r   r   r   <module>   s    