use std::sync::Arc;
use locode_protocol::{ContentBlock, Message, Report};
use locode_provider::Provider;
use locode_tools::Registry;
use tokio_util::sync::CancellationToken;
use crate::approve::{AllowAll, Approver};
use crate::config::EngineConfig;
use crate::sink::EventSink;
pub struct Session {
pub(crate) provider: Arc<dyn Provider>,
pub(crate) registry: Registry,
pub(crate) preamble: Vec<Message>,
pub(crate) config: EngineConfig,
pub(crate) sink: Box<dyn EventSink>,
pub(crate) cancel: CancellationToken,
pub(crate) history: Vec<Message>,
pub(crate) turns_run: u32,
pub(crate) approver: Arc<dyn Approver>,
pub(crate) last_instructions: Option<u64>,
pub(crate) skills_body: Option<String>,
}
impl Session {
#[must_use]
pub fn new(
provider: Arc<dyn Provider>,
registry: Registry,
preamble: Vec<Message>,
config: EngineConfig,
sink: Box<dyn EventSink>,
) -> Self {
Self {
provider,
registry,
history: preamble.clone(),
preamble,
config,
sink,
cancel: CancellationToken::new(),
turns_run: 0,
approver: Arc::new(AllowAll),
last_instructions: None,
skills_body: None,
}
}
#[must_use]
pub fn with_approver(mut self, approver: Arc<dyn Approver>) -> Self {
self.approver = approver;
self
}
#[must_use]
pub fn history(&self) -> &[Message] {
&self.history
}
#[must_use]
pub fn cancel_handle(&self) -> CancellationToken {
self.cancel.clone()
}
pub async fn run(&mut self, user: Vec<ContentBlock>) -> Report {
self.drive(user).await
}
pub async fn run_text(&mut self, prompt: impl Into<String>) -> Report {
self.run(vec![ContentBlock::Text {
text: prompt.into(),
}])
.await
}
}