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