
    oi	'                    F   d Z ddlmZ ddlZddlmZ ddlmZ ddlmZm	Z	 ddl
mZmZ ddlmZmZmZmZ dd	lmZ erdd
lmZ ddlmZ ddddddZd7dZd8dZ ed          fd9d Z ed          fd:d#Z	 d;d<d-Zd. ed          d/dfd=d3Zd. ed          edfd>d6Z dS )?z=Meeting event loop - orchestrates the multi-agent discussion.    )annotationsN)datetime)Path)TYPE_CHECKINGCallable)format_scratchpad_summary	run_agent)AgentConfigMeetingResultMeetingStateScratchpadEntry)run_pm)ContextManager)	LLMClientz[1;34mz[1;32mz[1;33mz[1;35mz[0m)pmagentsystemhumanresetlabelstrmessage	color_keyreturnNonec           	         t                               |d          }t           d         }t          | d|  d| d|            dS )z(Print a colored message to the terminal. r   [] N)_COLORSgetprint)r   r   r   colorr   s        2/root/projects/multi-agents-meeting/src/meeting.py_print_coloredr&      sQ    KK	2&&EGE	U
.
.U
.
.U
.
.W
.
./////    textc                    t          j        dd|                                           }t          j        dd|          }|dd                             d          S )z%Convert text to a filename-safe slug.z[^\w\s-]r   z[\s_]+-N2   )resublowerstrip)r(   slugs     r%   _slugifyr1   "   sJ    6+r4::<<00D6)S$''D9??3r'   reportsreporttopic
output_dirr   c                    |                     dd           t          j                                        d          }t	          |          }| d| d}||z  }|                    | d           |S )a  Save meeting report as a Markdown file.

    Args:
        report: The Markdown report content to save.
        topic: The meeting topic used to name the file.
        output_dir: Directory where the report file is written.

    Returns:
        The Path to the saved report file.
    Tparentsexist_ok%Y%m%d-%H%M%Sr*   z.mdutf-8encoding)mkdirr   nowstrftimer1   
write_text)r3   r4   r5   	timestampr0   filenamepaths          r%   save_reportrE   )   s|     TD111''88IE??D''d'''H DOOFWO---Kr'   resultr   c                    |                     dd           t          j                                        d          }t	          | j                  }| d| d}||z  }d| j         dg}| j        r|                    d| j         d           | j        D ]<}|                    d	|j	                    |                    |j
         d           =|                    d                    |          d
           |S )zSave meeting transcript as a Markdown file.

    Args:
        result: The complete MeetingResult with transcript data.
        output_dir: Directory where the transcript file is written.

    Returns:
        The Path to the saved transcript file.
    Tr7   r:   r*   z.transcript.mdu   # 会议讨论记录：
u   ## 背景

u	   ### 💬 r;   r<   )r>   r   r?   r@   r1   r4   contextappend
transcript
agent_namecontentrA   join)rF   r5   rB   r0   rC   rD   linesentrys           r%   save_transcriptrQ   =   s    TD111''88IFL!!D22d222H DB&,BBBCE~ 97V^777888" + +3!133444)))****OODIIe$$wO777Kr'   stater   agent_configsdict[str, AgentConfig]clientr   context_managerContextManager | None
str | Nonec                   | j         | j        k     r| xj         dz  c_         t          dd| j          d| j         dd           | j         | j        k    r=t          ddt	          j                              }| j                            |           t          | |||	          }t          d
d|j	         d           |j
        dk    r(t          d
dd           t                       |j        pdS |j        }||         }t          d
d| d|j         d           ||                    | ||j        |          }nt!          | j                  }t#          ||j        ||          }	t          |                                |	d           t                       t          ||	t	          j                              }
| j                            |
           | j         | j        k     dS )a  Execute one meeting loop until PM finishes or max_rounds reached.

    Args:
        state: The current MeetingState (modified in place).
        agent_configs: Map of agent name to AgentConfig.
        client: LLM client for PM and agents.
        context_manager: Optional ContextManager for whiteboard compression.

    Returns:
        The final report string if PM finished, or None if max_rounds reached.
       SYSTEMz
--- Round /z ---r   zRMAX ROUNDS REACHED. You MUST set next_action to FINISH and provide a final_report.rL   rM   rB   rV   PMz
Analysis: r   FINISHzMeeting concluded.&# Meeting Report

No report generated.zCalling z: N)rR   target_agent	pm_promptrU   )configpromptscratchpad_summaryrU   r   )current_round
max_roundsr&   r   r   r?   
scratchpadrJ   r   analysisnext_actionr#   final_reportrb   prompt_for_agentbuild_agent_contextr   r	   upper)rR   rS   rU   rV   force_entrydecisiontarget_nametarget_configrf   agent_responserP   s              r%   _run_meeting_loopru   Y   s4   " 
 0
0
0q x!Ze.A!Z!ZEDT!Z!Z!Z\deee%"222)#l",..  K
 ##K000%XXXt=(*;==tDDD8++4!5t<<<GGG(V,VV+%k2tRRRx7PRRTXYYY&!0!D!D("3	 "E " " ";5;K!L!L" ,1	
 
 
 	{((**NGDDD""lnn
 
 

 	&&&a 
 0
0
0d 4r'   
   r   rh   intrI   c           
        t          j                    }t          | ||          }||r|                    ||          |_        t          dd|  d           t          ddd                    |                                           d           t                       t          ||||          }	|	d	}	t          | |t          |                                          t          |j                  |	|t          j                    
          S )ar  Execute the full meeting loop.

    Args:
        topic: The discussion topic for the meeting.
        agent_configs: Map of agent name to AgentConfig for all participants.
        client: LLM client used by both PM and agents.
        max_rounds: Maximum number of PM decision rounds before forcing a finish.
        output_dir: Directory to save the final report (unused here; caller may use).
        context: Background context for the discussion.
        context_manager: Optional ContextManager for whiteboard compression.

    Returns:
        A MeetingResult containing the final report, transcript, and metadata.
    r4   rh   rI   Nr[   zMeeting started: r   Participants: , r^   zK# Meeting Report

Meeting ended after maximum rounds without PM conclusion.r4   rI   participantsrK   rl   
started_atfinished_at)r   r?   r   summarize_contextcontext_summaryr&   rN   keysr#   ru   r   listri   )
r4   rS   rU   rh   r5   rI   rV   r~   rR   r3   s
             r%   run_meetingr      s   . JuWMMME "w" / A A'6 R R8888(CCC8Odii8J8J8L8L.M.MOOQYZZZ	GGGumV_]]]F~`-,,..//())LNN   r'   input_fnCallable[[str], str]c           
        t          j                    }t          | ||          }	||r|                    ||          |	_        t          dd|  d           t          ddd                    |                                           d           t                       d}
	 t          |	|||
          }||}
t          dd            t          |
           t          d d            |d          }|
                                snzt          d|
                                t          j                              }|	j                            |           t          d|
                                d           d|	_        t          | |t!          |                                          t!          |	j                  |
|t          j                              S )aP  Execute an interactive meeting with human-in-the-loop feedback.

    Runs the meeting loop, then prompts the user for feedback. If feedback
    is provided, it is appended to the scratchpad and the loop continues.

    Args:
        topic: The discussion topic for the meeting.
        context: Background context for the discussion.
        agent_configs: Map of agent name to AgentConfig for all participants.
        client: LLM client used by both PM and agents.
        max_rounds: Maximum number of PM decision rounds per loop iteration.
        output_dir: Directory to save the final report.
        input_fn: Callable for getting user input (injected for testing).
        context_manager: Optional ContextManager for whiteboard compression.

    Returns:
        A MeetingResult containing the final report, transcript, and metadata.
    ry   Nr[   zInteractive meeting started: r   rz   r{   ra   Tr^   rH   z<============================================================u1   
💬 有补充内容吗？(直接回车结束): HUMANr]   r   r   r|   )r   r?   r   r   r   r&   rN   r   r#   ru   r/   r   ri   rJ   rg   r   r   )r4   rI   rS   rU   rh   r5   r   rV   r~   rR   r3   loop_report
user_inputhuman_entrys                 r%   run_meeting_interactiver      s   8 JuWMMME "w" / A A'6 R R8DUDDhOOO8Odii8J8J8L8L.M.MOOQYZZZ	GGG7F '}fVefff" Fo8oofoooXRSS
!! 	 &$$&&lnn
 
 

 	,,,w
 0 0 2 2G<<<  / 2 -,,..//())LNN   r'   )r   r   r   r   r   r   r   r   )r(   r   r   r   )r3   r   r4   r   r5   r   r   r   )rF   r   r5   r   r   r   )N)
rR   r   rS   rT   rU   r   rV   rW   r   rX   )r4   r   rS   rT   rU   r   rh   rw   r5   r   rI   r   rV   rW   r   r   )r4   r   rI   r   rS   rT   rU   r   rh   rw   r5   r   r   r   rV   rW   r   r   )!__doc__
__future__r   r,   r   pathlibr   typingr   r   	src.agentr   r	   
src.modelsr
   r   r   r   src.pmr   src.context_managerr   src.llm_clientr   r!   r&   r1   rE   rQ   ru   r   inputr    r'   r%   <module>r      s   C C " " " " " " 				             * * * * * * * * : : : : : : : : P P P P P P P P P P P P       )222222((((((  0 0 0 0        =ADOO     ( ?Cd9oo     @ .2	C C C C CT tI-1/ / / / /n tI%*-1J J J J J J Jr'   