ag_session/discovery.rs
1use crate::{AgentKind, AgentModel};
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 CLI-backed provider and
31 /// optional version.
32 pub fn new(kind: AgentKind, version: Option<String>) -> Self {
33 Self {
34 executable_name: kind.executable_name().unwrap_or_default(),
35 kind,
36 version: version.map_or(AgentCliVersion::Unknown, AgentCliVersion::Value),
37 }
38 }
39
40 /// Creates one CLI availability row for a CLI-backed provider whose
41 /// update/version refresh is still loading.
42 pub fn loading(kind: AgentKind) -> Self {
43 Self {
44 executable_name: kind.executable_name().unwrap_or_default(),
45 kind,
46 version: AgentCliVersion::Loading,
47 }
48 }
49
50 /// Builds unknown-version CLI rows for an existing provider availability
51 /// list.
52 pub fn from_kinds(agent_kinds: &[AgentKind]) -> Vec<Self> {
53 agent_kinds
54 .iter()
55 .copied()
56 .map(|agent_kind| Self::new(agent_kind, None))
57 .collect()
58 }
59
60 /// Builds loading CLI rows for an existing provider availability list
61 /// while the background update/version refresh is running.
62 pub fn loading_from_kinds(agent_kinds: &[AgentKind]) -> Vec<Self> {
63 agent_kinds.iter().copied().map(Self::loading).collect()
64 }
65}
66
67/// Detects which provider CLIs are locally runnable on the current machine.
68pub trait AgentAvailabilityProbe: Send + Sync {
69 /// Returns the agent kinds whose backing CLI executable is available.
70 fn available_agent_kinds(&self) -> Vec<AgentKind>;
71
72 /// Returns available agent CLI executables and their refreshed versions.
73 fn available_agent_clis(&self) -> Vec<AgentCliInfo> {
74 AgentCliInfo::from_kinds(&self.available_agent_kinds())
75 }
76
77 /// Returns the first [`AgentKind::Harness`] model whose provider
78 /// credentials are configured, or `None` when no provider is. Probes
79 /// without credential discovery report none.
80 fn native_harness_default_model(&self) -> Option<AgentModel> {
81 None
82 }
83}
84
85/// Availability probe that returns one caller-provided snapshot.
86pub struct StaticAgentAvailabilityProbe {
87 /// Agent kinds reported as available by the static probe.
88 pub available_agent_kinds: Vec<AgentKind>,
89}
90
91impl AgentAvailabilityProbe for StaticAgentAvailabilityProbe {
92 fn available_agent_kinds(&self) -> Vec<AgentKind> {
93 self.available_agent_kinds.clone()
94 }
95}
96
97#[cfg(test)]
98#[path = "discovery_test.rs"]
99mod tests;