From 6555aa881d1f40d9137433c4d9a84dc6fcfebbe3 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Thu, 10 Jul 2025 15:48:01 -0700 Subject: [PATCH] delete history --- codex-rs/core/src/codex.rs | 84 +++++++++++++++++++++++ codex-rs/core/src/conversation_history.rs | 5 ++ codex-rs/core/src/protocol.rs | 3 + 3 files changed, 92 insertions(+) diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 52c37c51ee..3a733407fc 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -782,6 +782,90 @@ async fn submission_loop( } }); } + + Op::compact {} => { + if let Some(sess) = sess.as_ref() { + let mut state = sess.state.lock().unwrap(); + if let Some(transcript) = state.zdr_transcript.as_mut() { + // Build the summary prompt + let summarization_input = ResponseItem::Message { + role: "system".to_string(), + content: vec![ContentItem::OutputText { + text: "Please summarize the conversation so far.".to_string(), + }], + }; + transcript.record_items(&[summarization_input]); + let turn_input = transcript.contents(); + drop(state); // Release lock before await + + // Run the turn and get the summary + let summary_text = match run_turn(sess, sub.id.clone(), turn_input).await { + Ok(items) => { + // Find the first assistant message in the output + items.iter().find_map(|item| match &item.item { + ResponseItem::Message { role, content } + if role == "assistant" => + { + content.iter().find_map(|c| { + if let ContentItem::OutputText { text } = c { + Some(text.clone()) + } else { + None + } + }) + } + _ => None, + }) + } + Err(e) => { + warn!("Failed to get summary: {e}"); + None + } + }; + + // Optionally: do something with summary_text, e.g., print or store + // For now, just clear the transcript as before + let mut state = sess.state.lock().unwrap(); + if let Some(transcript) = state.zdr_transcript.as_mut() { + // Instead of just clearing, construct a new transcript + *transcript = ConversationHistory::new(); + } + state.previous_response_id = None; + + // If we have a summary, submit a new Op::UserInput with the system message + if let Some(summary) = summary_text { + let instructions = sess.instructions.clone().unwrap_or_default(); + let system_message = crate::protocol::InputItem::Message { + role: "system".to_string(), + content: vec![crate::models::ContentItem::OutputText { + text: format!( + "{}\n\n[Summary of previous conversation]\n{}", + instructions, summary + ), + }], + }; + // Use the existing submit logic to inject this as a new user input + // This will spawn a new task and leverage all the normal machinery + let codex = Codex { + next_id: sess.next_id.clone(), + tx_sub: sess.tx_event.clone(), // Not ideal, but we don't have Codex here; in real code, refactor for access + rx_event: sess.tx_event.clone(), + }; + // In this context, we can't construct Codex, so instead, send directly to the submission queue if possible + // But we don't have access to tx_sub here. Instead, you should refactor to allow Op::compact to return a new Op::UserInput + // For now, just log a warning to indicate what should happen: + warn!( + "After compact, should submit Op::UserInput with system message: {}", + instructions + ); + // In a real refactor, you would do: + // codex.submit(Op::UserInput { items: vec![system_message] }).await; + } + } else { + state.previous_response_id = None; + } + } + } } } debug!("Agent loop exited"); diff --git a/codex-rs/core/src/conversation_history.rs b/codex-rs/core/src/conversation_history.rs index 52fb1ec4f4..91c020bb7b 100644 --- a/codex-rs/core/src/conversation_history.rs +++ b/codex-rs/core/src/conversation_history.rs @@ -22,6 +22,11 @@ impl ConversationHistory { self.items.clone() } + /// clears the conversation history. + pub(crate) fn clear(&mut self) { + self.items.clear(); + } + /// `items` is ordered from oldest to newest. pub(crate) fn record_items(&mut self, items: I) where diff --git a/codex-rs/core/src/protocol.rs b/codex-rs/core/src/protocol.rs index fa25a2fe38..d75454e6f6 100644 --- a/codex-rs/core/src/protocol.rs +++ b/codex-rs/core/src/protocol.rs @@ -108,6 +108,9 @@ pub enum Op { /// Request a single history entry identified by `log_id` + `offset`. GetHistoryEntryRequest { offset: usize, log_id: u64 }, + + /// Clear the message history. + compact, } /// Determines the conditions under which the user is consulted to approve