use rap_protocol::ThreadId;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use async_trait::async_trait;
use crate::tools::Tool;
use crate::traits::{ConversationStore, InputSender, StateStore};
use rap_client::http::HttpClient;
use super::driver::ThreadLifecycleEvent;
use super::handle::{HandleSubscribeRequest, ThreadHandle, attach, handle_observer_factory};
use super::router::RunningSystem;
use super::sender::ChannelSender;
use crate::system::builder::{AgentSystem, LocalAgentSystem};
use crate::system::config::{ThreadConfig, ThreadConfigSource};
use crate::system::model::{ModelSource, ResolvedModel};
type BoxError = Box<dyn std::error::Error + Send + Sync>;
struct LaunchConfig<M: InputSender> {
tools: Vec<Rc<dyn Tool<M>>>,
extra_system_prompt: Option<String>,
model: Option<Rc<dyn ModelSource>>,
}
#[derive(Clone)]
pub(crate) struct LaunchRegistry<M: InputSender> {
threads: Rc<RefCell<HashMap<ThreadId, LaunchConfig<M>>>>,
}
impl<M: InputSender> Default for LaunchRegistry<M> {
fn default() -> Self {
Self {
threads: Rc::new(RefCell::new(HashMap::new())),
}
}
}
async fn root_thread_id<C: ConversationStore>(
store: &C,
thread_id: &ThreadId<str>,
) -> Result<ThreadId, BoxError> {
Ok(store
.get_ancestor_chain(thread_id)
.await
.map_err(|e| Box::new(e) as BoxError)?
.first()
.map(|(id, _)| id.clone())
.unwrap_or_else(|| thread_id.to_owned()))
}
pub(crate) struct UnionModelSource<C: ConversationStore, M: InputSender> {
pub(crate) inner: Box<dyn ModelSource>,
pub(crate) registry: LaunchRegistry<M>,
pub(crate) conversation_store: C,
}
#[async_trait(?Send)]
impl<C, M> ModelSource for UnionModelSource<C, M>
where
C: ConversationStore + 'static,
M: InputSender + 'static,
{
async fn resolve(&self, thread_id: &ThreadId<str>) -> Result<ResolvedModel, BoxError> {
let root_id = root_thread_id(&self.conversation_store, thread_id).await?;
let launched = self
.registry
.threads
.borrow()
.get(&root_id)
.and_then(|config| config.model.clone());
match launched {
Some(model) => model.resolve(thread_id).await,
None => self.inner.resolve(thread_id).await,
}
}
}
pub(crate) struct UnionConfigSource<C: ConversationStore, M: InputSender, H: HttpClient> {
pub(crate) inner: Box<dyn ThreadConfigSource<M, H>>,
pub(crate) registry: LaunchRegistry<M>,
pub(crate) conversation_store: C,
}
#[async_trait(?Send)]
impl<C, M, H> ThreadConfigSource<M, H> for UnionConfigSource<C, M, H>
where
C: ConversationStore + 'static,
M: InputSender + 'static,
H: HttpClient + 'static,
{
async fn resolve(&self, thread_id: &ThreadId<str>) -> Result<ThreadConfig<M, H>, BoxError> {
let mut config = self.inner.resolve(thread_id).await?;
let root_id = root_thread_id(&self.conversation_store, thread_id).await?;
if let Some(local) = self.registry.threads.borrow().get(&root_id) {
config.tools.extend(local.tools.iter().cloned());
config.extra_system_prompt = match (
config.extra_system_prompt.take(),
local.extra_system_prompt.clone(),
) {
(Some(system_wide), Some(local)) => Some(format!("{system_wide}\n\n{local}")),
(system_wide, local) => system_wide.or(local),
};
}
Ok(config)
}
}
impl<C, S, H> LocalAgentSystem<C, S, H>
where
C: ConversationStore + 'static,
S: StateStore + 'static,
H: HttpClient + 'static,
{
pub fn start(self) -> LaunchingSystem<C, S, H> {
let registry = self.registry.clone();
let system = AgentSystem {
inner: self.system.inner.clone(),
};
let running = self.start_inner(handle_observer_factory());
LaunchingSystem {
system,
running,
registry,
}
}
}
pub struct LaunchingSystem<C, S, H>
where
C: ConversationStore,
S: StateStore,
H: HttpClient,
{
pub(crate) system: AgentSystem<C, S, ChannelSender, H>,
pub(crate) running: RunningSystem<HandleSubscribeRequest>,
pub(crate) registry: LaunchRegistry<ChannelSender>,
}
impl<C, S, H> LaunchingSystem<C, S, H>
where
C: ConversationStore + 'static,
S: StateStore + 'static,
H: HttpClient + 'static,
{
pub fn thread_builder(&self) -> ThreadBuilder<'_, C, S, H> {
ThreadBuilder {
system: self,
tools: Vec::new(),
extra_system_prompt: None,
model: None,
}
}
pub async fn thread_handle(
&self,
thread_id: &ThreadId<str>,
) -> Result<Option<ThreadHandle>, C::Error> {
if !self.thread_exists(thread_id).await? {
return Ok(None);
}
Ok(Some(attach(&self.running, thread_id).await))
}
async fn thread_exists(&self, thread_id: &ThreadId<str>) -> Result<bool, C::Error> {
if self.registry.threads.borrow().contains_key(thread_id) {
return Ok(true);
}
self.system
.inner
.conversation_store
.thread_exists(thread_id)
.await
}
pub fn sender(&self) -> ChannelSender {
self.running.sender()
}
pub async fn next_lifecycle_event(&mut self) -> Option<ThreadLifecycleEvent> {
self.running.next_lifecycle_event().await
}
pub fn is_idle(&self) -> bool {
self.running.is_idle()
}
pub async fn shutdown(self) {
self.running.shutdown().await;
}
}
pub struct ThreadBuilder<'a, C, S, H>
where
C: ConversationStore,
S: StateStore,
H: HttpClient,
{
system: &'a LaunchingSystem<C, S, H>,
tools: Vec<Box<dyn Tool<ChannelSender>>>,
extra_system_prompt: Option<String>,
model: Option<Rc<dyn ModelSource>>,
}
impl<C, S, H> ThreadBuilder<'_, C, S, H>
where
C: ConversationStore + 'static,
S: StateStore + 'static,
H: HttpClient + 'static,
{
pub fn tool(mut self, tool: Box<dyn Tool<ChannelSender>>) -> Self {
self.tools.push(tool);
self
}
pub fn tools(mut self, tools: impl IntoIterator<Item = Box<dyn Tool<ChannelSender>>>) -> Self {
self.tools.extend(tools);
self
}
pub fn extra_system_prompt(mut self, prompt: impl Into<String>) -> Self {
self.extra_system_prompt = Some(prompt.into());
self
}
pub fn model(mut self, model: impl ModelSource + 'static) -> Self {
self.model = Some(Rc::new(model));
self
}
pub async fn launch(self) -> ThreadHandle {
let thread_id = ThreadId::from(uuid::Uuid::new_v4().to_string());
self.system.registry.threads.borrow_mut().insert(
thread_id.clone(),
LaunchConfig {
tools: self.tools.into_iter().map(Rc::from).collect(),
extra_system_prompt: self.extra_system_prompt,
model: self.model,
},
);
attach(&self.system.running, &thread_id).await
}
}
#[cfg(test)]
mod tests {
use crate::system::test_support::*;
#[tokio::test(flavor = "current_thread")]
async fn launched_threads_union_their_own_tools_and_prompt() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (system, mut ctrl) = start_launcher_system(
vec![Box::new(NamedTool("global_tool"))],
"Global prompt.",
);
let mut alpha = system
.thread_builder()
.tool(Box::new(NamedTool("alpha_tool")))
.extra_system_prompt("You are alpha.")
.launch()
.await;
let mut beta = system
.thread_builder()
.tool(Box::new(NamedTool("beta_tool")))
.launch()
.await;
assert_ne!(alpha.thread_id(), beta.thread_id());
alpha.send_user_text("hi").await.expect("send input");
let req = ctrl.next_request().await;
let names = tool_names(&req);
assert!(names.contains(&"global_tool".to_owned()));
assert!(names.contains(&"alpha_tool".to_owned()));
assert!(!names.contains(&"beta_tool".to_owned()));
let preamble = req.preamble.clone().expect("system prompt present");
assert!(preamble.contains("Global prompt."));
assert!(preamble.contains("You are alpha."));
ctrl.send_text("hello alpha");
ctrl.finish();
assert_eq!(
handle_texts_until_finished(&mut alpha).await,
["hello alpha"]
);
beta.send_user_text("hi").await.expect("send input");
let req = ctrl.next_request().await;
let names = tool_names(&req);
assert!(names.contains(&"global_tool".to_owned()));
assert!(names.contains(&"beta_tool".to_owned()));
assert!(!names.contains(&"alpha_tool".to_owned()));
let preamble = req.preamble.clone().expect("system prompt present");
assert!(preamble.contains("Global prompt."));
assert!(!preamble.contains("You are alpha."));
ctrl.send_text("hello beta");
ctrl.finish();
assert_eq!(handle_texts_until_finished(&mut beta).await, ["hello beta"]);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn launcher_thread_handle_attaches_to_existing_threads_only() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (mut system, mut ctrl) = start_launcher_system(vec![], "");
assert!(
system
.thread_handle(rap_protocol::ThreadId::from_ref("no-such-thread"))
.await
.expect("check unknown thread")
.is_none()
);
let launched = system.thread_builder().launch().await;
let launched_id = launched.thread_id().to_owned();
assert!(
system
.thread_handle(&launched_id)
.await
.expect("check launched thread")
.is_some()
);
let history_thread = system.thread_builder().launch().await;
let history_thread_id = history_thread.thread_id().to_owned();
history_thread
.send_user_text("hello")
.await
.expect("send input");
let _req = ctrl.next_request().await;
ctrl.send_text("hi");
ctrl.finish();
while !system.is_idle() {
tokio::time::timeout(
std::time::Duration::from_secs(5),
system.next_lifecycle_event(),
)
.await
.expect("timed out waiting for a driver exit")
.expect("thread lifecycle channel closed");
}
drop(history_thread);
let mut attached = system
.thread_handle(&history_thread_id)
.await
.expect("load existing thread")
.expect("thread has history");
assert!(
!attached.replay().history.is_empty(),
"replay includes the thread's history"
);
attached.send_user_text("again").await.expect("send input");
let _req = ctrl.next_request().await;
ctrl.send_text("still here");
ctrl.finish();
assert_eq!(
handle_texts_until_finished(&mut attached).await,
["still here"]
);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn launched_thread_children_inherit_launch_config() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (system, mut ctrl) = start_launcher_system(
vec![Box::new(NamedTool("global_tool"))],
"Global prompt.",
);
let alpha = system
.thread_builder()
.tool(Box::new(NamedTool("alpha_tool")))
.extra_system_prompt("You are alpha.")
.launch()
.await;
let alpha_id = alpha.thread_id().to_owned();
alpha
.send_user_text("spawn a child")
.await
.expect("send input");
let _req = ctrl.next_request().await;
ctrl.send_tool_call(
"tc-spawn",
"spawn_thread",
serde_json::json!({
"instructions": "do child work",
"child_of": [alpha_id],
}),
);
ctrl.finish();
let parent_followup = ctrl.next_request().await;
assert!(tool_names(&parent_followup).contains(&"alpha_tool".to_owned()));
ctrl.send_text("spawned");
ctrl.finish();
let child_req = ctrl.next_request().await;
let names = tool_names(&child_req);
assert!(names.contains(&"global_tool".to_owned()));
assert!(
names.contains(&"alpha_tool".to_owned()),
"child threads inherit the launched root's tools"
);
let preamble = child_req.preamble.expect("system prompt present");
assert!(
preamble.contains("You are alpha."),
"child threads inherit the launched root's prompt"
);
ctrl.send_text("child done");
ctrl.finish();
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn launched_threads_use_their_own_model_and_children_inherit_it() {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
let (system, mut system_ctrl) = start_launcher_system(vec![], "");
let (alpha_model, mut alpha_ctrl) = model_source(None);
let alpha = system.thread_builder().model(alpha_model).launch().await;
let alpha_id = alpha.thread_id().to_owned();
let other = system.thread_builder().launch().await;
alpha.send_user_text("hi").await.expect("send input");
let _req = alpha_ctrl.next_request().await;
alpha_ctrl.send_text("from alpha model");
alpha_ctrl.finish();
other.send_user_text("hi").await.expect("send input");
let _req = system_ctrl.next_request().await;
system_ctrl.send_text("from system model");
system_ctrl.finish();
alpha
.send_user_text("spawn a child")
.await
.expect("send input");
let _req = alpha_ctrl.next_request().await;
alpha_ctrl.send_tool_call(
"tc-spawn",
"spawn_thread",
serde_json::json!({
"instructions": "do child work",
"child_of": [alpha_id],
}),
);
alpha_ctrl.finish();
let _parent_followup = alpha_ctrl.next_request().await;
alpha_ctrl.send_text("spawned");
alpha_ctrl.finish();
let child_req = alpha_ctrl.next_request().await;
assert!(
tool_result_texts(&child_req)
.iter()
.any(|t| t.contains("do child work")),
"the request on alpha's model is the child's seed"
);
alpha_ctrl.send_text("child done");
alpha_ctrl.finish();
})
.await;
}
}