@@ -172,16 +172,10 @@ impl ConversationState {
172172 }
173173
174174 /// Sets the response message according to the currently set [Self::next_message].
175- // pub fn push_assistant_message(&mut self, message: AssistantResponseMessage) {
176175 pub fn push_assistant_message ( & mut self , message : AssistantMessage ) {
177176 debug_assert ! ( self . next_message. is_some( ) , "next_message should exist" ) ;
178177 let next_user_message = self . next_message . take ( ) . expect ( "next user message should exist" ) ;
179178
180- // // Don't include the tool spec in all user messages in the history.
181- // if let Some(ctx) = next_user_message.user_input_message_context.as_mut() {
182- // ctx.tools.take();
183- // }
184-
185179 self . append_assistant_transcript ( & message) ;
186180 self . history . push_back ( ( next_user_message, message) ) ;
187181 }
@@ -276,26 +270,29 @@ impl ConversationState {
276270 }
277271
278272 /// Returns a [FigConversationState] capable of being sent by [fig_api_client::StreamingClient].
279- pub async fn as_sendable_conversation_state ( & mut self ) -> FigConversationState {
273+ ///
274+ /// Params:
275+ /// - `run_hooks` - whether hooks should be executed and included as context
276+ pub async fn as_sendable_conversation_state ( & mut self , run_hooks : bool ) -> FigConversationState {
280277 debug_assert ! ( self . next_message. is_some( ) ) ;
281278 self . enforce_conversation_invariants ( ) ;
282279 self . history . drain ( self . valid_history_range . 1 ..) ;
283280 self . history . drain ( ..self . valid_history_range . 0 ) ;
284281
285- self . backend_conversation_state ( false )
282+ self . backend_conversation_state ( run_hooks , false )
286283 . await
287284 . into_fig_conversation_state ( )
288285 . expect ( "unable to construct conversation state" )
289286 }
290287
291288 /// Returns a conversation state representation which reflects the exact conversation to send
292289 /// back to the model.
293- pub async fn backend_conversation_state ( & mut self , quiet : bool ) -> BackendConversationState < ' _ > {
290+ pub async fn backend_conversation_state ( & mut self , run_hooks : bool , quiet : bool ) -> BackendConversationState < ' _ > {
294291 self . enforce_conversation_invariants ( ) ;
295292
296293 // Run hooks and add to conversation start and next user message.
297294 let mut conversation_start_context = None ;
298- if let Some ( cm) = self . context_manager . as_mut ( ) {
295+ if let ( true , Some ( cm) ) = ( run_hooks , self . context_manager . as_mut ( ) ) {
299296 let mut null_writer = SharedWriter :: null ( ) ;
300297 let updates = if quiet {
301298 None
@@ -371,7 +368,7 @@ impl ConversationState {
371368 } ,
372369 } ;
373370
374- let conv_state = self . backend_conversation_state ( true ) . await ;
371+ let conv_state = self . backend_conversation_state ( false , true ) . await ;
375372
376373 // Include everything but the last message in the history.
377374 let history_len = conv_state. history . len ( ) ;
@@ -505,7 +502,7 @@ impl ConversationState {
505502
506503 /// Calculate the total character count in the conversation
507504 pub async fn calculate_char_count ( & mut self ) -> CharCount {
508- self . backend_conversation_state ( true ) . await . char_count ( )
505+ self . backend_conversation_state ( false , true ) . await . char_count ( )
509506 }
510507
511508 /// Get the current token warning level
625622 let mut user_input_message: UserInputMessage = self
626623 . next_user_message
627624 . cloned ( )
628- . map ( Into :: into )
625+ . map ( UserMessage :: into_user_input_message )
629626 . ok_or ( eyre:: eyre!( "next user message is not set" ) ) ?;
630627 if let Some ( ctx) = user_input_message. user_input_message_context . as_mut ( ) {
631628 ctx. tools = Some ( self . tools . to_vec ( ) ) ;
638635 } )
639636 }
640637
641- pub fn get_utilization ( & self ) -> ConversationSize {
638+ pub fn calculate_conversation_size ( & self ) -> ConversationSize {
642639 let mut user_chars = 0 ;
643640 let mut assistant_chars = 0 ;
644641 let mut context_chars = 0 ;
@@ -684,7 +681,7 @@ where
684681 T : Iterator < Item = & ' a ( UserMessage , AssistantMessage ) > ,
685682{
686683 history. fold ( Vec :: new ( ) , |mut acc, ( user, assistant) | {
687- acc. push ( ChatMessage :: UserInputMessage ( user. clone ( ) . into ( ) ) ) ;
684+ acc. push ( ChatMessage :: UserInputMessage ( user. clone ( ) . into_history_entry ( ) ) ) ;
688685 acc. push ( ChatMessage :: AssistantResponseMessage ( assistant. clone ( ) . into ( ) ) ) ;
689686 acc
690687 } )
@@ -839,7 +836,7 @@ mod tests {
839836 // User -> Assistant -> User -> Assistant ...and so on.
840837 conversation_state. set_next_user_message ( "start" . to_string ( ) ) . await ;
841838 for i in 0 ..=( MAX_CONVERSATION_STATE_HISTORY_LEN + 100 ) {
842- let s = conversation_state. as_sendable_conversation_state ( ) . await ;
839+ let s = conversation_state. as_sendable_conversation_state ( true ) . await ;
843840 assert_conversation_state_invariants ( s, i) ;
844841 conversation_state. push_assistant_message ( AssistantMessage :: new_response ( None , i. to_string ( ) ) ) ;
845842 conversation_state. set_next_user_message ( i. to_string ( ) ) . await ;
@@ -853,7 +850,7 @@ mod tests {
853850 ConversationState :: new ( Context :: new_fake ( ) , load_tools ( ) . unwrap ( ) , None , None ) . await ;
854851 conversation_state. set_next_user_message ( "start" . to_string ( ) ) . await ;
855852 for i in 0 ..=( MAX_CONVERSATION_STATE_HISTORY_LEN + 100 ) {
856- let s = conversation_state. as_sendable_conversation_state ( ) . await ;
853+ let s = conversation_state. as_sendable_conversation_state ( true ) . await ;
857854 assert_conversation_state_invariants ( s, i) ;
858855
859856 conversation_state. push_assistant_message ( AssistantMessage :: new_tool_use ( None , i. to_string ( ) , vec ! [
@@ -875,7 +872,7 @@ mod tests {
875872 ConversationState :: new ( Context :: new_fake ( ) , load_tools ( ) . unwrap ( ) , None , None ) . await ;
876873 conversation_state. set_next_user_message ( "start" . to_string ( ) ) . await ;
877874 for i in 0 ..=( MAX_CONVERSATION_STATE_HISTORY_LEN + 100 ) {
878- let s = conversation_state. as_sendable_conversation_state ( ) . await ;
875+ let s = conversation_state. as_sendable_conversation_state ( true ) . await ;
879876 assert_conversation_state_invariants ( s, i) ;
880877 if i % 3 == 0 {
881878 conversation_state. push_assistant_message ( AssistantMessage :: new_tool_use ( None , i. to_string ( ) , vec ! [
@@ -908,7 +905,7 @@ mod tests {
908905 // User -> Assistant -> User -> Assistant ...and so on.
909906 conversation_state. set_next_user_message ( "start" . to_string ( ) ) . await ;
910907 for i in 0 ..=( MAX_CONVERSATION_STATE_HISTORY_LEN + 100 ) {
911- let s = conversation_state. as_sendable_conversation_state ( ) . await ;
908+ let s = conversation_state. as_sendable_conversation_state ( true ) . await ;
912909
913910 // Ensure that the first two messages are the fake context messages.
914911 let hist = s. history . as_ref ( ) . unwrap ( ) ;
@@ -965,7 +962,7 @@ mod tests {
965962 // Simulate conversation flow
966963 conversation_state. set_next_user_message ( "start" . to_string ( ) ) . await ;
967964 for i in 0 ..=5 {
968- let s = conversation_state. as_sendable_conversation_state ( ) . await ;
965+ let s = conversation_state. as_sendable_conversation_state ( true ) . await ;
969966 let hist = s. history . as_ref ( ) . unwrap ( ) ;
970967 #[ allow( clippy:: match_wildcard_for_single_variants) ]
971968 match & hist[ 0 ] {
0 commit comments