use std::path::PathBuf;
use std::sync::Arc;
use ag_contracts::{
AgentChannel, AgentError, AgentFuture, OneShotClient, OneShotError, OneShotRequest,
OneShotSubmission, TurnEvent, TurnRequest, TurnResult,
};
use ag_session::AgentKind;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
#[derive(Clone, Default)]
pub struct RuntimeFactory {
app_server: Option<Arc<dyn ag_agent::AppServerClient>>,
native_harness: Option<ag_agent::NativeHarnessConfig>,
}
impl RuntimeFactory {
#[must_use]
pub fn with_native_harness(mut self, data_root: PathBuf) -> Self {
self.native_harness = Some(ag_agent::NativeHarnessConfig::new(data_root));
self
}
pub fn session(&self, kind: AgentKind) -> SessionRuntime {
SessionRuntime {
channel: ag_agent::create_agent_channel(
kind,
self.app_server.clone(),
self.native_harness.as_ref(),
),
}
}
pub fn utility(&self) -> UtilityRuntime {
let client = ag_agent::RealOneShotClient::pooled(self.app_server.clone());
let client = match &self.native_harness {
Some(config) => client.with_native_harness(config.clone()),
None => client,
};
UtilityRuntime {
client: Arc::new(client),
}
}
}
#[derive(Clone)]
pub struct SessionRuntime {
channel: Arc<dyn AgentChannel>,
}
impl SessionRuntime {
pub fn run_turn(
&self,
id: String,
request: TurnRequest,
events: mpsc::UnboundedSender<TurnEvent>,
) -> AgentFuture<Result<TurnResult, AgentError>> {
self.channel.run_turn(id, request, events)
}
pub fn shutdown(&self, id: String) -> AgentFuture<Result<(), AgentError>> {
self.channel.shutdown_session(id)
}
}
pub struct UtilityRuntime {
client: Arc<dyn OneShotClient>,
}
impl UtilityRuntime {
pub async fn submit_cancellable(
&self,
request: OneShotRequest,
cancellation: CancellationToken,
) -> Result<OneShotSubmission, OneShotError> {
self.client.submit_cancellable(request, cancellation).await
}
pub async fn close(&self) {
self.client.close().await;
}
pub fn force_shutdown(&self) {
self.client.force_shutdown();
}
}
#[cfg(feature = "test-utils")]
#[path = "test_support_test.rs"]
pub mod test_support;