ag_session/discovery.rs
1use crate::AgentKind;
2
3/// Automatic update and version probe state for one locally runnable agent
4/// CLI.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum AgentCliVersion {
7 /// Startup update plus version detection is still running in the
8 /// background.
9 Loading,
10 /// Version detection finished, but the executable did not report a usable
11 /// version.
12 Unknown,
13 /// Version detection finished with a parsed display value.
14 Value(String),
15}
16
17/// One locally runnable agent CLI and the installed version refreshed at
18/// startup.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct AgentCliInfo {
21 /// Executable name used to launch the provider CLI.
22 pub executable_name: &'static str,
23 /// Agent provider family backed by the executable.
24 pub kind: AgentKind,
25 /// Current automatic update and version probe state for this executable.
26 pub version: AgentCliVersion,
27}
28
29impl AgentCliInfo {
30 /// Creates one CLI availability row for a provider and optional version.
31 pub fn new(kind: AgentKind, version: Option<String>) -> Self {
32 Self {
33 executable_name: kind.executable_name(),
34 kind,
35 version: version.map_or(AgentCliVersion::Unknown, AgentCliVersion::Value),
36 }
37 }
38
39 /// Creates one CLI availability row whose update/version refresh is still
40 /// loading.
41 pub fn loading(kind: AgentKind) -> Self {
42 Self {
43 executable_name: kind.executable_name(),
44 kind,
45 version: AgentCliVersion::Loading,
46 }
47 }
48
49 /// Builds unknown-version CLI rows for an existing provider availability
50 /// list.
51 pub fn from_kinds(agent_kinds: &[AgentKind]) -> Vec<Self> {
52 agent_kinds
53 .iter()
54 .copied()
55 .map(|agent_kind| Self::new(agent_kind, None))
56 .collect()
57 }
58
59 /// Builds loading CLI rows for an existing provider availability list
60 /// while the background update/version refresh is running.
61 pub fn loading_from_kinds(agent_kinds: &[AgentKind]) -> Vec<Self> {
62 agent_kinds.iter().copied().map(Self::loading).collect()
63 }
64}
65
66/// Detects which provider CLIs are locally runnable on the current machine.
67pub trait AgentAvailabilityProbe: Send + Sync {
68 /// Returns the agent kinds whose backing CLI executable is available.
69 fn available_agent_kinds(&self) -> Vec<AgentKind>;
70
71 /// Returns available agent CLI executables and their refreshed versions.
72 fn available_agent_clis(&self) -> Vec<AgentCliInfo> {
73 AgentCliInfo::from_kinds(&self.available_agent_kinds())
74 }
75}
76
77/// Availability probe that returns one caller-provided snapshot.
78pub struct StaticAgentAvailabilityProbe {
79 /// Agent kinds reported as available by the static probe.
80 pub available_agent_kinds: Vec<AgentKind>,
81}
82
83impl AgentAvailabilityProbe for StaticAgentAvailabilityProbe {
84 fn available_agent_kinds(&self) -> Vec<AgentKind> {
85 self.available_agent_kinds.clone()
86 }
87}
88
89#[cfg(test)]
90#[path = "discovery_test.rs"]
91mod tests;