magi-code 0.62.0

Repository-aware CLI coding agent for terminal work
Documentation
use super::ShellState;

pub(super) fn handle_compact_builtin(
    arg: Option<&str>,
    state: &mut ShellState,
) -> anyhow::Result<String> {
    let Some(session) = state.current_session.clone() else {
        anyhow::bail!(
            "/compact requires an active persisted session; start without --no-session or run /new"
        )
    };
    let mut active_config = state
        .config
        .clone()
        .ok_or_else(|| anyhow::anyhow!("/compact requires loaded runtime config"))?;
    active_config.model = Some(state.model.clone());
    let settings = crate::config::read_settings(&active_config.paths)?;
    if let Some(auth_state) = crate::compaction::refresh_active_auth_for_compaction_if_inherited(
        &mut active_config,
        &settings,
        &state.auth_state,
    )? {
        state.auth_state = auth_state;
    }
    if let Some(config) = &mut state.config {
        config.auth = active_config.auth.clone();
        config.model = active_config.model.clone();
    }
    let result = crate::compaction::compact_session(crate::compaction::CompactSessionJob {
        active_config,
        settings,
        session,
        cwd: state.cwd.clone(),
        cancellation: crate::agent::cancellation::AgentCancellation::default(),
        custom_instructions: arg.map(str::to_string),
    })?;
    match result {
        Some(result) => Ok(crate::compaction::compaction_success_message(&result)),
        None => Ok("compaction produced empty summary; no checkpoint was written".to_string()),
    }
}