diff --git a/codex-rs/codex-api/src/endpoint/compact.rs b/codex-rs/codex-api/src/endpoint/compact.rs index 2b02ebd0f0..969083bb10 100644 --- a/codex-rs/codex-api/src/endpoint/compact.rs +++ b/codex-rs/codex-api/src/endpoint/compact.rs @@ -45,7 +45,7 @@ impl CompactClient { } } - pub async fn compact( + async fn compact( &self, body: serde_json::Value, extra_headers: HeaderMap, diff --git a/codex-rs/codex-api/src/endpoint/responses.rs b/codex-rs/codex-api/src/endpoint/responses.rs index 8b9938b549..0caf280657 100644 --- a/codex-rs/codex-api/src/endpoint/responses.rs +++ b/codex-rs/codex-api/src/endpoint/responses.rs @@ -50,10 +50,7 @@ impl ResponsesClient { } } - pub async fn stream_request( - &self, - request: ResponsesRequest, - ) -> Result { + async fn stream_request(&self, request: ResponsesRequest) -> Result { self.stream(request.body, request.headers).await } @@ -95,6 +92,7 @@ impl ResponsesClient { } } + // Pub mainly for testing purpose. pub async fn stream( &self, body: Value, diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rs index 3c7a645a18..6948e02dc8 100644 --- a/codex-rs/core/src/client.rs +++ b/codex-rs/core/src/client.rs @@ -322,9 +322,10 @@ impl ModelClient { /// `ResponseItem`s representing the compacted transcript. pub async fn compact_conversation_history( &self, - prompt: &PromptBuilder, + input: &[ResponseItem], + instructions: &str, ) -> Result> { - if prompt.input.is_empty() { + if input.is_empty() { return Ok(Vec::new()); } let auth_manager = self.auth_manager.clone(); @@ -338,10 +339,11 @@ impl ModelClient { let client = ApiCompactClient::new(transport, api_provider, api_auth) .with_telemetry(Some(request_telemetry)); - let instructions = prompt - .get_full_instructions(&self.config.model_family) - .into_owned(); - let payload = prompt.build_compaction_input(&self.config.model, &instructions); + let payload = codex_api::CompactionInput { + model: &self.config.model, + input, + instructions, + }; let mut extra_headers = ApiHeaderMap::new(); if let SessionSource::SubAgent(sub) = &self.session_source { diff --git a/codex-rs/core/src/client_common.rs b/codex-rs/core/src/client_common.rs index 4d28deea29..27826f81bd 100644 --- a/codex-rs/core/src/client_common.rs +++ b/codex-rs/core/src/client_common.rs @@ -5,7 +5,6 @@ use crate::model_family::ModelFamily; use crate::model_provider_info::WireApi; use crate::tools::spec::create_tools_json_for_chat_completions_api; use crate::tools::spec::create_tools_json_for_responses_api; -use codex_api::CompactionInput; use codex_api::Prompt; pub use codex_api::common::ResponseEvent; use codex_apply_patch::APPLY_PATCH_TOOL_INSTRUCTIONS; @@ -153,18 +152,6 @@ impl PromptBuilder { }) } - pub(crate) fn build_compaction_input<'a>( - &'a self, - model: &'a str, - instructions: &'a str, - ) -> CompactionInput<'a> { - CompactionInput { - model, - input: &self.input, - instructions, - } - } - pub(crate) fn get_full_instructions<'a>(&'a self, model: &'a ModelFamily) -> Cow<'a, str> { let base = self .base_instructions_override diff --git a/codex-rs/core/src/compact_remote.rs b/codex-rs/core/src/compact_remote.rs index 9809e658b0..5498c6e69a 100644 --- a/codex-rs/core/src/compact_remote.rs +++ b/codex-rs/core/src/compact_remote.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use crate::client_common::PromptBuilder; +use crate::PromptBuilder; use crate::codex::Session; use crate::codex::TurnContext; use crate::error::Result as CodexResult; @@ -9,6 +9,7 @@ use crate::protocol::ContextCompactedEvent; use crate::protocol::EventMsg; use crate::protocol::RolloutItem; use crate::protocol::TaskStartedEvent; +use codex_apply_patch::APPLY_PATCH_TOOL_INSTRUCTIONS; use codex_protocol::models::ResponseItem; pub(crate) async fn run_inline_remote_auto_compact_task( @@ -41,14 +42,14 @@ async fn run_remote_compact_task_inner_impl( turn_context: &Arc, ) -> CodexResult<()> { let mut history = sess.clone_history().await; - let mut prompt = PromptBuilder::new() - .wire_api(turn_context.client.get_provider().wire_api) - .with_input(history.get_history_for_prompt()); - prompt.base_instructions_override = turn_context.base_instructions.clone(); + let prompt = PromptBuilder::new() + .with_input(history.get_history_for_prompt()) + .with_base_instructions_override_opt(turn_context.base_instructions.clone()) + .build(&turn_context.client.get_model_family())?; let mut new_history = turn_context .client - .compact_conversation_history(&prompt) + .compact_conversation_history(&prompt.input, &prompt.instructions) .await?; // Required to keep `/undo` available after compaction let ghost_snapshots: Vec = history