mirror of
https://github.com/openai/codex.git
synced 2026-09-15 12:08:01 +00:00
Cleaning for compaction
This commit is contained in:
@@ -45,7 +45,7 @@ impl<T: HttpTransport, A: AuthProvider> CompactClient<T, A> {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn compact(
|
||||
async fn compact(
|
||||
&self,
|
||||
body: serde_json::Value,
|
||||
extra_headers: HeaderMap,
|
||||
|
||||
@@ -50,10 +50,7 @@ impl<T: HttpTransport, A: AuthProvider> ResponsesClient<T, A> {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn stream_request(
|
||||
&self,
|
||||
request: ResponsesRequest,
|
||||
) -> Result<ResponseStream, ApiError> {
|
||||
async fn stream_request(&self, request: ResponsesRequest) -> Result<ResponseStream, ApiError> {
|
||||
self.stream(request.body, request.headers).await
|
||||
}
|
||||
|
||||
@@ -95,6 +92,7 @@ impl<T: HttpTransport, A: AuthProvider> ResponsesClient<T, A> {
|
||||
}
|
||||
}
|
||||
|
||||
// Pub mainly for testing purpose.
|
||||
pub async fn stream(
|
||||
&self,
|
||||
body: Value,
|
||||
|
||||
@@ -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<Vec<ResponseItem>> {
|
||||
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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<TurnContext>,
|
||||
) -> 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<ResponseItem> = history
|
||||
|
||||
Reference in New Issue
Block a user