
    j                         d Z ddlmZmZmZmZmZ ddlmZm	Z	 ddl
mZ 	 	 	 	 ddeded	ee   d
edee   dee   dedeeef   fdZy)z$Multi-turn simulation functionality.    )CallableOptionalDictAnyList)
id_helperstrack   )SimulatedUserNappuser_simulatorinitial_message	max_turns	thread_idproject_name
app_kwargsreturnc                 L   |t        j                         }t        | d      s,t        | d      r| j                  nd} t	        |      |       } g }||j                  |      }t        |      D ]  }	|	dk(  r|}
n|j                  |      }
d|
d}|j                  |       	  | |
fd	|i|d
d||	dz   |ddii}t        |t              rd|vsd|vrd|rt        |      ndd}|j                  |        |||dS # t        $ r}ddt        |       d}Y d}~bd}~ww xY w)a  
    Run a multi-turn conversation simulation between a simulated user and an app.

    1. The simulator passes single message strings to the app
    2. The app manages full conversation history internally using thread_id
    3. The app logs traces with thread_id for evaluation

    Args:
        app: Callable that processes messages and manages conversation history internally.
            Signature: app(message: str, *, thread_id: str, **kwargs) -> Dict[str, str]
            The app is automatically decorated with @track and thread_id is injected via opik_args.
        user_simulator: SimulatedUser instance that generates user responses
        initial_message: Optional initial message from the user. If None, generated by simulator
        max_turns: Maximum number of conversation turns (default: 5)
        thread_id: Optional thread ID for grouping traces. Generated if not provided
        project_name: Optional project name for trace logging
        **app_kwargs: Additional keyword arguments passed to the app

    Returns:
        Dict containing:
        - thread_id: The thread ID used for this simulation
        - conversation_history: List of message dicts from the simulation
        - project_name: Project name if provided
    Nopik_tracked__name__simulation_app)namer   user)rolecontentr   	opik_argstracer
   )turnr   )r   metadata	assistantzError processing message: r   r   zNo response)r   conversation_historyr   )r   generate_idhasattrr   r	   generate_responserangeappend	Exceptionstr
isinstancedict)r   r   r   r   r   r   r   app_namer!   r   user_message_textuser_messageassistant_messagees                 r/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/simulation/simulator.pyrun_simulationr1      s   D **,	 3'#*3
#;3<<AQ"e"3' 24 (::;OP i 19 / . @ @AU V !'3DE##L1	 #!
!#
! 
!
 %.-1AX|$T	
!( ,d3.. 11 $$ 01"	! 	##$56Y !^  4$ -  	 $7Ax@!	s   "C??	D#DD#)N   NN)__doc__typingr   r   r   r   r   opikr   r	   simulated_userr   r(   intr1        r0   <module>r:      s    * 6 6 " ) &*#"&d	d!d c]d 	d
 }d 3-d d 
#s(^dr9   