1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! Discovery of locally runnable agent CLIs, harness models, and `ChatGPT`
//! plan sign-in.
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())
}
}
/// 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;