ag-session 0.22.1

Agentty is an ADE (Agentic Development Environment) for structured, controllable AI-assisted software development.
Documentation
//! Discovery of locally runnable agent CLIs, harness models, `ChatGPT` plan
//! sign-in, and harness API key storage.

use async_trait::async_trait;

use crate::{AgentKind, HarnessModelOption};

/// Automatic update and version probe state for one locally runnable agent
/// CLI.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AgentCliVersion {
    /// Startup update plus version detection is still running in the
    /// background.
    Loading,
    /// Version detection finished, but the executable did not report a usable
    /// version.
    Unknown,
    /// Version detection finished with a parsed display value.
    Value(String),
}

/// One locally runnable agent CLI and the installed version refreshed at
/// startup.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AgentCliInfo {
    /// Executable name used to launch the provider CLI.
    pub executable_name: &'static str,
    /// Agent provider family backed by the executable.
    pub kind: AgentKind,
    /// Current automatic update and version probe state for this executable.
    pub version: AgentCliVersion,
}

impl AgentCliInfo {
    /// Creates one CLI availability row for a CLI-backed provider and
    /// optional version.
    pub fn new(kind: AgentKind, version: Option<String>) -> Self {
        Self {
            executable_name: kind.executable_name().unwrap_or_default(),
            kind,
            version: version.map_or(AgentCliVersion::Unknown, AgentCliVersion::Value),
        }
    }

    /// Creates one CLI availability row for a CLI-backed provider whose
    /// update/version refresh is still loading.
    pub fn loading(kind: AgentKind) -> Self {
        Self {
            executable_name: kind.executable_name().unwrap_or_default(),
            kind,
            version: AgentCliVersion::Loading,
        }
    }

    /// Builds unknown-version CLI rows for an existing provider availability
    /// list.
    pub fn from_kinds(agent_kinds: &[AgentKind]) -> Vec<Self> {
        agent_kinds
            .iter()
            .copied()
            .map(|agent_kind| Self::new(agent_kind, None))
            .collect()
    }

    /// Builds loading CLI rows for an existing provider availability list
    /// while the background update/version refresh is running.
    pub fn loading_from_kinds(agent_kinds: &[AgentKind]) -> Vec<Self> {
        agent_kinds.iter().copied().map(Self::loading).collect()
    }
}

/// Receives the browser URL of a `ChatGPT` sign-in once it is ready for the
/// user, so the host can open it and show it as a fallback.
pub type ChatGptSignInUrlHandler = Box<dyn FnOnce(String) + Send>;

/// Account that a completed `ChatGPT` plan sign-in stored.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChatGptSignInOutcome {
    /// Signed-in account email, when the provider reports one.
    pub email: Option<String>,
}

/// Detects which provider CLIs and harness models are runnable on the
/// current machine.
#[async_trait]
pub trait AgentAvailabilityProbe: Send + Sync {
    /// Returns the agent kinds whose backing CLI executable is available.
    fn available_agent_kinds(&self) -> Vec<AgentKind>;

    /// Returns available agent CLI executables and their refreshed versions.
    fn available_agent_clis(&self) -> Vec<AgentCliInfo> {
        AgentCliInfo::from_kinds(&self.available_agent_kinds())
    }

    /// Returns the [`AgentKind::Harness`] models a user can choose: models
    /// the local OpenAI-compatible server serves, then the hosted catalog
    /// models with their configuration status. Implementations bound every
    /// network probe with short timeouts. Probes without harness discovery
    /// report none.
    async fn native_harness_models(&self) -> Vec<HarnessModelOption> {
        Vec::new()
    }

    /// Runs the browser sign-in that lets `chatgpt/…` harness models use
    /// the user's `ChatGPT` plan, passing its URL to `on_url` before
    /// waiting for the browser callback. Probes without harness discovery
    /// do not support it.
    ///
    /// # Errors
    /// Returns a user-facing message when the sign-in cannot start, is
    /// denied, or cannot be stored.
    async fn sign_in_chatgpt(
        &self,
        _on_url: ChatGptSignInUrlHandler,
    ) -> Result<ChatGptSignInOutcome, String> {
        Err("ChatGPT sign-in is unavailable in this launch.".to_string())
    }

    /// Stores `api_key` as the key harness models of `provider`, such as
    /// `kimi`, use from now on, replacing a stored one. Probes without
    /// harness discovery do not support it.
    ///
    /// # Errors
    /// Returns a user-facing message when the provider takes no API key or
    /// the key cannot be stored.
    async fn save_harness_api_key(&self, _provider: &str, _api_key: &str) -> Result<(), String> {
        Err("API keys cannot be saved in this launch.".to_string())
    }
}

/// Availability probe that returns one caller-provided snapshot.
pub struct StaticAgentAvailabilityProbe {
    /// Agent kinds reported as available by the static probe.
    pub available_agent_kinds: Vec<AgentKind>,
}

impl AgentAvailabilityProbe for StaticAgentAvailabilityProbe {
    fn available_agent_kinds(&self) -> Vec<AgentKind> {
        self.available_agent_kinds.clone()
    }
}

#[cfg(test)]
#[path = "discovery_test.rs"]
mod tests;