Skip to main content

ag_runtime/
provider.rs

1//! Machine-scoped provider discovery and session workspace setup and cleanup
2//! through the concrete adapter registry.
3
4use std::path::Path;
5
6use ag_contracts::AgentError;
7use ag_session::{
8    AgentAvailabilityProbe, AgentCliInfo, AgentKind, ChatGptSignInOutcome, ChatGptSignInUrlHandler,
9    HarnessModelOption,
10};
11use async_trait::async_trait;
12
13/// Machine-scoped provider discovery through the concrete adapter registry.
14pub struct RealAgentAvailabilityProbe;
15
16#[async_trait]
17impl AgentAvailabilityProbe for RealAgentAvailabilityProbe {
18    fn available_agent_kinds(&self) -> Vec<AgentKind> {
19        ag_agent::RealAgentAvailabilityProbe.available_agent_kinds()
20    }
21
22    fn available_agent_clis(&self) -> Vec<AgentCliInfo> {
23        ag_agent::RealAgentAvailabilityProbe.available_agent_clis()
24    }
25
26    async fn native_harness_models(&self) -> Vec<HarnessModelOption> {
27        ag_agent::RealAgentAvailabilityProbe
28            .native_harness_models()
29            .await
30    }
31
32    async fn sign_in_chatgpt(
33        &self,
34        on_url: ChatGptSignInUrlHandler,
35    ) -> Result<ChatGptSignInOutcome, String> {
36        ag_agent::RealAgentAvailabilityProbe
37            .sign_in_chatgpt(on_url)
38            .await
39    }
40
41    async fn save_harness_api_key(&self, provider: &str, api_key: &str) -> Result<(), String> {
42        ag_agent::RealAgentAvailabilityProbe
43            .save_harness_api_key(provider, api_key)
44            .await
45    }
46}
47
48/// Initializes provider resources in a session workspace.
49///
50/// # Errors
51/// Returns a normalized adapter setup failure.
52pub fn setup_backend(kind: AgentKind, folder: &Path) -> Result<(), AgentError> {
53    ag_agent::create_backend(kind)
54        .setup(folder)
55        .map_err(Into::into)
56}
57
58/// Whether the selected provider retains a process between turns.
59pub fn uses_persistent_session(kind: AgentKind) -> bool {
60    ag_agent::transport_mode(kind).uses_app_server()
61}
62
63/// Whether the selected provider's session turns accept user prompts while
64/// running.
65pub fn supports_turn_steering(kind: AgentKind) -> bool {
66    ag_agent::supports_turn_steering(kind)
67}
68
69/// Reclaims adapter-owned resources during workspace deletion.
70///
71/// # Errors
72/// Returns the runtime artifact cleanup failure.
73pub fn cleanup_session_worktree_artifacts(folder: &Path) -> Result<(), AgentError> {
74    ag_agent::cleanup_session_worktree_artifacts(folder).map_err(Into::into)
75}