Skip to main content

claude_code_agent/
compaction.rs

1//! Manual context compaction with custom instructions.
2//!
3//! `/compact <instructions>` sent as a user turn makes the CLI summarize the
4//! conversation, steering the summary with `instructions`. The turn's stream
5//! yields `AgentEvent::Compacted` and ends with `Done`.
6//!
7//! Hooks see it too: PreCompact receives `trigger: "manual"` and
8//! `custom_instructions`; PostCompact receives `compact_summary`.
9//! Source: binary PreCompact / PostCompact hook input schemas,
10//!         system/compact_boundary schema
11use crate::agent::{Agent, AgentError, AgentStream};
12
13/// Compact the session's context now.
14pub async fn compact(agent: &Agent, instructions: Option<&str>) -> Result<AgentStream, AgentError> {
15    let command = match instructions {
16        Some(i) if !i.trim().is_empty() => format!("/compact {}", i.trim()),
17        _ => "/compact".to_string(),
18    };
19    agent.prompt(command).await
20}