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
42/// Initializes provider resources in a session workspace.
43///
44/// # Errors
45/// Returns a normalized adapter setup failure.
46pub fn setup_backend(kind: AgentKind, folder: &Path) -> Result<(), AgentError> {
47    ag_agent::create_backend(kind)
48        .setup(folder)
49        .map_err(Into::into)
50}
51
52/// Whether the selected provider retains a process between turns.
53pub fn uses_persistent_session(kind: AgentKind) -> bool {
54    ag_agent::transport_mode(kind).uses_app_server()
55}
56
57/// Reclaims adapter-owned resources during workspace deletion.
58///
59/// # Errors
60/// Returns the runtime artifact cleanup failure.
61pub fn cleanup_session_worktree_artifacts(folder: &Path) -> Result<(), AgentError> {
62    ag_agent::cleanup_session_worktree_artifacts(folder).map_err(Into::into)
63}