magi-code 0.96.1

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

pub(crate) struct AgentRunRequest<'a, 'sink> {
    pub(crate) prompt: &'a str,
    pub(crate) prompt_origin: crate::output::UserPromptOrigin,
    pub(crate) effective_prompt: Option<&'a str>,
    pub(crate) tools: Option<&'a ToolRuntime>,
    pub(crate) hooks: Option<&'a HookRuntime>,
    pub(crate) session: Option<&'a Session>,
    pub(crate) cwd: &'a Path,
    pub(crate) output_sink: Option<&'sink mut dyn AgentOutputSink>,
    pub(crate) cancellation: AgentCancellation,
    pub(crate) session_title_job: Option<crate::session_titles::SessionTitleJob>,
    pub(crate) semantic_progress_timeout: Option<Duration>,
    pub(crate) invocation_mode: InvocationMode,
    pub(crate) agent_id: Option<String>,
    pub(crate) initial_instructions: &'a [InstructionFile],
    pub(crate) herdr_reporter: Option<HerdrReporter>,
    pub(crate) continuation_auto_compaction_policy: Option<(usize, String)>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ManualCompactionRequest {
    pub(crate) context: Option<String>,
}

pub(crate) fn filtered_herdr_reporter(
    invocation_mode: InvocationMode,
    reporter: Option<HerdrReporter>,
) -> Option<HerdrReporter> {
    (!matches!(invocation_mode, InvocationMode::Subagent))
        .then_some(reporter)
        .flatten()
}

pub(crate) fn run_with_herdr_turn<T>(
    invocation_mode: InvocationMode,
    reporter: Option<HerdrReporter>,
    body: impl FnOnce(Option<HerdrReporter>) -> anyhow::Result<T>,
) -> anyhow::Result<T> {
    let reporter = filtered_herdr_reporter(invocation_mode, reporter);
    let mut turn = HerdrTurnReporter::start(reporter.clone());
    let result = body(reporter);
    turn.finish_result(&result);
    result
}
pub(crate) fn report_preflight_blocked(
    invocation_mode: InvocationMode,
    reporter: Option<HerdrReporter>,
) {
    report_preflight_outcome(invocation_mode, reporter, false);
}

pub(crate) fn report_preflight_cancelled(
    invocation_mode: InvocationMode,
    reporter: Option<HerdrReporter>,
) {
    report_preflight_outcome(invocation_mode, reporter, true);
}

fn report_preflight_outcome(
    invocation_mode: InvocationMode,
    reporter: Option<HerdrReporter>,
    cancelled: bool,
) {
    let mut turn = HerdrTurnReporter::start(filtered_herdr_reporter(invocation_mode, reporter));
    if cancelled {
        turn.finish_cancelled();
    } else {
        turn.finish_blocked();
    }
}