use std::sync::Arc;
use crate::config::TurnRunOptions;
use crate::conversation::ConversationState;
use crate::driver::TurnRunner;
use super::checkpoint::SessionCheckpoint;
use super::execution::SessionCore;
use super::handle::SessionHandle;
use super::types::{SessionConfig, SessionError};
pub struct SessionBuildRejection {
pub error: SessionError,
pub state: ConversationState,
pub runner: Arc<TurnRunner>,
pub options: TurnRunOptions,
pub config: SessionConfig,
}
impl std::fmt::Debug for SessionBuildRejection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SessionBuildRejection")
.field("error", &self.error)
.field("state", &self.state)
.field("runner", &"Arc<TurnRunner>")
.field("options", &self.options)
.field("config", &self.config)
.finish()
}
}
pub struct Session {
core: Arc<SessionCore>,
}
impl std::fmt::Debug for Session {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Session")
.field("conversation_id", &self.core.conversation_id())
.finish_non_exhaustive()
}
}
impl Session {
pub(super) fn from_core(core: Arc<SessionCore>) -> Session {
Session { core }
}
#[allow(clippy::result_large_err)]
pub fn new(
state: ConversationState,
runner: Arc<TurnRunner>,
options: TurnRunOptions,
config: SessionConfig,
) -> Result<Session, SessionBuildRejection> {
if state.active_turn().is_some() || state.sealed_result().is_some() {
return Err(SessionBuildRejection {
error: SessionError::InvalidInput(
"session construction requires an idle state (empty active slot)".into(),
),
state,
runner,
options,
config,
});
}
Ok(Session {
core: SessionCore::new(state, runner, options, config),
})
}
pub fn handle(&self) -> SessionHandle {
SessionHandle {
core: Arc::clone(&self.core),
}
}
pub fn checkpoint(&self) -> Result<SessionCheckpoint, SessionError> {
self.core.checkpoint()
}
pub async fn shutdown(&self) {
self.core.close();
self.core.quiesce().await;
}
}
impl Drop for Session {
fn drop(&mut self) {
self.core.close();
}
}