Skip to main content

infinity_agent_core/system/
config.rs

1//! Per-thread agent configuration: tools, system prompt, and tool-server
2//! notifier, resolved when a thread is loaded.
3
4use rap_protocol::ThreadId;
5use std::rc::Rc;
6
7use async_trait::async_trait;
8
9use crate::tools::Tool;
10use crate::traits::InputSender;
11use rap_client::http::HttpClient;
12use rap_client::notifier::RapNotifier;
13
14type BoxError = Box<dyn std::error::Error + Send + Sync>;
15
16/// The configuration a thread runs with. Resolved by a
17/// [`ThreadConfigSource`] each time a thread is loaded, so
18/// different threads of one system can see different toolsets (e.g. one
19/// daemon session per working directory, each with its own RAP servers).
20///
21/// Tools are reference-counted so an embedding can hand the same tool
22/// instances to every thread of a session while retaining its own handles
23/// (e.g. to shut down the server behind a tool when the session idles).
24pub struct ThreadConfig<M: InputSender, H: HttpClient> {
25    pub tools: Vec<Rc<dyn Tool<M>>>,
26    /// Appended to the built-in system prompt.
27    pub extra_system_prompt: Option<String>,
28    /// Best-effort lifecycle notifications (tool cancellation, thread
29    /// closure) to this thread's RAP tool servers.
30    pub rap_notifier: Option<RapNotifier<H>>,
31}
32
33/// Resolves the configuration for each thread as it is loaded.
34///
35/// Like [`ModelSource`](super::ModelSource), resolution happens per thread
36/// load rather than once at system construction, which is what lets a single
37/// long-lived system serve many differently-configured conversations.
38#[async_trait(?Send)]
39pub trait ThreadConfigSource<M: InputSender, H: HttpClient> {
40    async fn resolve(&self, thread_id: &ThreadId<str>) -> Result<ThreadConfig<M, H>, BoxError>;
41}
42
43/// The same fixed configuration for every thread — what
44/// [`AgentSystemBuilder`](super::AgentSystemBuilder)'s `tools` /
45/// `extra_system_prompt` / `rap_notifier` methods build.
46pub struct StaticThreadConfig<M: InputSender, H: HttpClient> {
47    pub tools: Vec<Rc<dyn Tool<M>>>,
48    pub extra_system_prompt: Option<String>,
49    pub rap_notifier: Option<RapNotifier<H>>,
50}
51
52#[async_trait(?Send)]
53impl<M: InputSender + 'static, H: HttpClient + 'static> ThreadConfigSource<M, H>
54    for StaticThreadConfig<M, H>
55{
56    async fn resolve(&self, _thread_id: &ThreadId<str>) -> Result<ThreadConfig<M, H>, BoxError> {
57        Ok(ThreadConfig {
58            tools: self.tools.clone(),
59            extra_system_prompt: self.extra_system_prompt.clone(),
60            rap_notifier: self.rap_notifier.clone(),
61        })
62    }
63}