Compacts the conversation history using the strategy specified by [CompactStrategy], replacing the history with a summary generated by the model. If the compact request itself fails, it will be retried depending on [CompactStrategy] If [CompactStrategy::messages_to_exclude] is greater than 0, and [CompactStrategy::truncate_large_messages] is true, then compaction will not be retried and will fai
(
&mut self,
os: &mut Os,
custom_prompt: Option<String>,
show_summary: bool,
strategy: CompactStrategy,
)
| 1495 | /// [CompactStrategy::truncate_large_messages] is true, then compaction will not be retried and |
| 1496 | /// will fail with [ChatError::CompactHistoryFailure]. |
| 1497 | async fn compact_history( |
| 1498 | &mut self, |
| 1499 | os: &mut Os, |
| 1500 | custom_prompt: Option<String>, |
| 1501 | show_summary: bool, |
| 1502 | strategy: CompactStrategy, |
| 1503 | ) -> Result<ChatState, ChatError> { |
| 1504 | // Same pattern as is done for handle_response for getting request metadata on sigint. |
| 1505 | let request_metadata: Arc<Mutex<Option<RequestMetadata>>> = Arc::new(Mutex::new(None)); |
| 1506 | let request_metadata_clone = Arc::clone(&request_metadata); |
| 1507 | let mut ctrl_c_stream = self.ctrlc_rx.resubscribe(); |
| 1508 | |
| 1509 | tokio::select! { |
| 1510 | res = self.compact_history_impl(os, custom_prompt, show_summary, strategy, request_metadata_clone) => res, |
| 1511 | Ok(_) = ctrl_c_stream.recv() => { |
| 1512 | debug!(?request_metadata, "ctrlc received in compact history"); |
| 1513 | // Wait for handle_response to finish handling the ctrlc. |
| 1514 | tokio::time::sleep(Duration::from_millis(5)).await; |
| 1515 | if let Some(request_metadata) = request_metadata.lock().await.take() { |
| 1516 | self.user_turn_request_metadata.push(request_metadata); |
| 1517 | } |
| 1518 | self.send_chat_telemetry( |
| 1519 | os, |
| 1520 | TelemetryResult::Cancelled, |
| 1521 | None, |
| 1522 | None, |
| 1523 | None, |
| 1524 | true, |
| 1525 | ) |
| 1526 | .await; |
| 1527 | Err(ChatError::Interrupted { tool_uses: Some(self.tool_uses.clone()) }) |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | async fn compact_history_impl( |
| 1533 | &mut self, |