1use std::path::PathBuf;
2use std::sync::Arc;
3
4use ag_contracts::{
5 AgentChannel, AgentError, AgentFuture, OneShotClient, OneShotError, OneShotRequest,
6 OneShotSubmission, TurnEvent, TurnRequest, TurnResult,
7};
8use ag_session::AgentKind;
9use tokio::sync::mpsc;
10use tokio_util::sync::CancellationToken;
11
12#[derive(Clone, Default)]
14pub struct RuntimeFactory {
15 app_server: Option<Arc<dyn ag_agent::AppServerClient>>,
16 native_harness: Option<ag_agent::NativeHarnessConfig>,
17}
18
19impl RuntimeFactory {
20 #[must_use]
24 pub fn with_native_harness(mut self, data_root: PathBuf) -> Self {
25 self.native_harness = Some(ag_agent::NativeHarnessConfig::new(data_root));
26
27 self
28 }
29
30 pub fn session(&self, kind: AgentKind) -> SessionRuntime {
32 SessionRuntime {
33 channel: ag_agent::create_agent_channel(
34 kind,
35 self.app_server.clone(),
36 self.native_harness.as_ref(),
37 ),
38 }
39 }
40
41 pub fn utility(&self) -> UtilityRuntime {
43 let client = ag_agent::RealOneShotClient::pooled(self.app_server.clone());
44 let client = match &self.native_harness {
45 Some(config) => client.with_native_harness(config.clone()),
46 None => client,
47 };
48
49 UtilityRuntime {
50 client: Arc::new(client),
51 }
52 }
53}
54
55#[derive(Clone)]
57pub struct SessionRuntime {
58 channel: Arc<dyn AgentChannel>,
59}
60
61impl SessionRuntime {
62 pub fn run_turn(
64 &self,
65 id: String,
66 request: TurnRequest,
67 events: mpsc::UnboundedSender<TurnEvent>,
68 ) -> AgentFuture<Result<TurnResult, AgentError>> {
69 self.channel.run_turn(id, request, events)
70 }
71
72 pub fn shutdown(&self, id: String) -> AgentFuture<Result<(), AgentError>> {
74 self.channel.shutdown_session(id)
75 }
76}
77
78pub struct UtilityRuntime {
80 client: Arc<dyn OneShotClient>,
81}
82
83impl UtilityRuntime {
84 pub async fn submit_cancellable(
90 &self,
91 request: OneShotRequest,
92 cancellation: CancellationToken,
93 ) -> Result<OneShotSubmission, OneShotError> {
94 self.client.submit_cancellable(request, cancellation).await
95 }
96
97 pub async fn close(&self) {
99 self.client.close().await;
100 }
101
102 pub fn force_shutdown(&self) {
104 self.client.force_shutdown();
105 }
106}
107
108#[cfg(feature = "test-utils")]
109#[path = "test_support_test.rs"]
110pub mod test_support;