Skip to main content

ag_session/
discovery.rs

1//! Discovery of locally runnable agent CLIs, harness models, `ChatGPT` plan
2//! sign-in, and harness API key storage.
3
4use async_trait::async_trait;
5
6use crate::{AgentKind, HarnessModelOption};
7
8/// Automatic update and version probe state for one locally runnable agent
9/// CLI.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum AgentCliVersion {
12    /// Startup update plus version detection is still running in the
13    /// background.
14    Loading,
15    /// Version detection finished, but the executable did not report a usable
16    /// version.
17    Unknown,
18    /// Version detection finished with a parsed display value.
19    Value(String),
20}
21
22/// One locally runnable agent CLI and the installed version refreshed at
23/// startup.
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct AgentCliInfo {
26    /// Executable name used to launch the provider CLI.
27    pub executable_name: &'static str,
28    /// Agent provider family backed by the executable.
29    pub kind: AgentKind,
30    /// Current automatic update and version probe state for this executable.
31    pub version: AgentCliVersion,
32}
33
34impl AgentCliInfo {
35    /// Creates one CLI availability row for a CLI-backed provider and
36    /// optional version.
37    pub fn new(kind: AgentKind, version: Option<String>) -> Self {
38        Self {
39            executable_name: kind.executable_name().unwrap_or_default(),
40            kind,
41            version: version.map_or(AgentCliVersion::Unknown, AgentCliVersion::Value),
42        }
43    }
44
45    /// Creates one CLI availability row for a CLI-backed provider whose
46    /// update/version refresh is still loading.
47    pub fn loading(kind: AgentKind) -> Self {
48        Self {
49            executable_name: kind.executable_name().unwrap_or_default(),
50            kind,
51            version: AgentCliVersion::Loading,
52        }
53    }
54
55    /// Builds unknown-version CLI rows for an existing provider availability
56    /// list.
57    pub fn from_kinds(agent_kinds: &[AgentKind]) -> Vec<Self> {
58        agent_kinds
59            .iter()
60            .copied()
61            .map(|agent_kind| Self::new(agent_kind, None))
62            .collect()
63    }
64
65    /// Builds loading CLI rows for an existing provider availability list
66    /// while the background update/version refresh is running.
67    pub fn loading_from_kinds(agent_kinds: &[AgentKind]) -> Vec<Self> {
68        agent_kinds.iter().copied().map(Self::loading).collect()
69    }
70}
71
72/// Receives the browser URL of a `ChatGPT` sign-in once it is ready for the
73/// user, so the host can open it and show it as a fallback.
74pub type ChatGptSignInUrlHandler = Box<dyn FnOnce(String) + Send>;
75
76/// Account that a completed `ChatGPT` plan sign-in stored.
77#[derive(Debug, Clone, PartialEq, Eq)]
78pub struct ChatGptSignInOutcome {
79    /// Signed-in account email, when the provider reports one.
80    pub email: Option<String>,
81}
82
83/// Detects which provider CLIs and harness models are runnable on the
84/// current machine.
85#[async_trait]
86pub trait AgentAvailabilityProbe: Send + Sync {
87    /// Returns the agent kinds whose backing CLI executable is available.
88    fn available_agent_kinds(&self) -> Vec<AgentKind>;
89
90    /// Returns available agent CLI executables and their refreshed versions.
91    fn available_agent_clis(&self) -> Vec<AgentCliInfo> {
92        AgentCliInfo::from_kinds(&self.available_agent_kinds())
93    }
94
95    /// Returns the [`AgentKind::Harness`] models a user can choose: models
96    /// the local OpenAI-compatible server serves, then the hosted catalog
97    /// models with their configuration status. Implementations bound every
98    /// network probe with short timeouts. Probes without harness discovery
99    /// report none.
100    async fn native_harness_models(&self) -> Vec<HarnessModelOption> {
101        Vec::new()
102    }
103
104    /// Runs the browser sign-in that lets `chatgpt/…` harness models use
105    /// the user's `ChatGPT` plan, passing its URL to `on_url` before
106    /// waiting for the browser callback. Probes without harness discovery
107    /// do not support it.
108    ///
109    /// # Errors
110    /// Returns a user-facing message when the sign-in cannot start, is
111    /// denied, or cannot be stored.
112    async fn sign_in_chatgpt(
113        &self,
114        _on_url: ChatGptSignInUrlHandler,
115    ) -> Result<ChatGptSignInOutcome, String> {
116        Err("ChatGPT sign-in is unavailable in this launch.".to_string())
117    }
118
119    /// Stores `api_key` as the key harness models of `provider`, such as
120    /// `kimi`, use from now on, replacing a stored one. Probes without
121    /// harness discovery do not support it.
122    ///
123    /// # Errors
124    /// Returns a user-facing message when the provider takes no API key or
125    /// the key cannot be stored.
126    async fn save_harness_api_key(&self, _provider: &str, _api_key: &str) -> Result<(), String> {
127        Err("API keys cannot be saved in this launch.".to_string())
128    }
129}
130
131/// Availability probe that returns one caller-provided snapshot.
132pub struct StaticAgentAvailabilityProbe {
133    /// Agent kinds reported as available by the static probe.
134    pub available_agent_kinds: Vec<AgentKind>,
135}
136
137impl AgentAvailabilityProbe for StaticAgentAvailabilityProbe {
138    fn available_agent_kinds(&self) -> Vec<AgentKind> {
139        self.available_agent_kinds.clone()
140    }
141}
142
143#[cfg(test)]
144#[path = "discovery_test.rs"]
145mod tests;