1use std::path::PathBuf;
5use std::sync::Arc;
6
7use ag_contracts::{
8 AgentChannel, AgentError, AgentFuture, OneShotClient, OneShotError, OneShotRequest,
9 OneShotSubmission, TurnEvent, TurnRequest, TurnResult,
10};
11use ag_protocol::TurnPrompt;
12use ag_session::AgentKind;
13use tokio::sync::mpsc;
14use tokio_util::sync::CancellationToken;
15
16#[derive(Clone, Default)]
18pub struct RuntimeFactory {
19 app_server: Option<Arc<dyn ag_agent::AppServerClient>>,
20 native_harness: Option<ag_agent::NativeHarnessConfig>,
21}
22
23impl RuntimeFactory {
24 #[must_use]
28 pub fn with_native_harness(mut self, data_root: PathBuf) -> Self {
29 self.native_harness = Some(ag_agent::NativeHarnessConfig::new(data_root));
30
31 self
32 }
33
34 pub fn session(&self, kind: AgentKind) -> SessionRuntime {
36 SessionRuntime {
37 channel: ag_agent::create_agent_channel(
38 kind,
39 self.app_server.clone(),
40 self.native_harness.as_ref(),
41 ),
42 }
43 }
44
45 pub fn utility(&self) -> UtilityRuntime {
47 let client = ag_agent::RealOneShotClient::pooled(self.app_server.clone());
48 let client = match &self.native_harness {
49 Some(config) => client.with_native_harness(config.clone()),
50 None => client,
51 };
52
53 UtilityRuntime {
54 client: Arc::new(client),
55 }
56 }
57}
58
59#[derive(Clone)]
61pub struct SessionRuntime {
62 channel: Arc<dyn AgentChannel>,
63}
64
65impl SessionRuntime {
66 pub fn run_turn(
68 &self,
69 id: String,
70 request: TurnRequest,
71 events: mpsc::UnboundedSender<TurnEvent>,
72 ) -> AgentFuture<Result<TurnResult, AgentError>> {
73 self.channel.run_turn(id, request, events)
74 }
75
76 pub fn steer_turn(
79 &self,
80 id: String,
81 prompt: TurnPrompt,
82 ) -> AgentFuture<Result<(), AgentError>> {
83 self.channel.steer_turn(id, prompt)
84 }
85
86 pub fn shutdown(&self, id: String) -> AgentFuture<Result<(), AgentError>> {
88 self.channel.shutdown_session(id)
89 }
90}
91
92pub struct UtilityRuntime {
94 client: Arc<dyn OneShotClient>,
95}
96
97impl UtilityRuntime {
98 pub async fn submit_cancellable(
104 &self,
105 request: OneShotRequest,
106 cancellation: CancellationToken,
107 ) -> Result<OneShotSubmission, OneShotError> {
108 self.client.submit_cancellable(request, cancellation).await
109 }
110
111 pub async fn close(&self) {
113 self.client.close().await;
114 }
115
116 pub fn force_shutdown(&self) {
118 self.client.force_shutdown();
119 }
120}
121
122#[cfg(feature = "test-utils")]
123#[path = "test_support_test.rs"]
124pub mod test_support;