Skip to content

Commit 3dae3f8

Browse files
committed
Add agent registration to main event loop and update API state
- Introduced a new channel for registering newly created agents in the main event loop, enhancing the agent management system. - Updated the `ApiState` structure to include a sender for agent registration, allowing agents to be sent from the API to the main loop. - Modified the `create_agent` function to send newly created agents to the main loop, improving the integration of agent lifecycle management.
1 parent 5172741 commit 3dae3f8

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/api/server.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,11 +899,25 @@ async fn create_agent(
899899
);
900900
}
901901

902+
// Build the Agent and send to main loop so it can receive messages
903+
let sqlite_pool = db.sqlite.clone();
904+
let mut deps_with_cron = deps.clone();
905+
deps_with_cron.cron_tool = Some(cron_tool);
906+
let agent = crate::Agent {
907+
id: arc_agent_id.clone(),
908+
config: agent_config.clone(),
909+
db,
910+
deps: deps_with_cron,
911+
};
912+
if let Err(error) = state.agent_tx.send(agent).await {
913+
tracing::error!(%error, "failed to send new agent to main loop");
914+
}
915+
902916
// Update all ArcSwap-based API state maps
903917
{
904918
// Agent pools
905919
let mut pools = (**state.agent_pools.load()).clone();
906-
pools.insert(agent_id.clone(), db.sqlite.clone());
920+
pools.insert(agent_id.clone(), sqlite_pool);
907921
state.agent_pools.store(std::sync::Arc::new(pools));
908922

909923
// Memory searches

src/api/state.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ pub struct ApiState {
8282
pub prompt_engine: RwLock<Option<PromptEngine>>,
8383
/// Instance-level defaults for resolving new agent configs.
8484
pub defaults_config: RwLock<Option<DefaultsConfig>>,
85+
/// Sender to register newly created agents with the main event loop.
86+
pub agent_tx: mpsc::Sender<crate::Agent>,
8587
}
8688

8789
/// Events sent to SSE clients. Wraps ProcessEvents with agent context.
@@ -163,7 +165,10 @@ pub enum ApiEvent {
163165
}
164166

165167
impl ApiState {
166-
pub fn new_with_provider_sender(provider_setup_tx: mpsc::Sender<crate::ProviderSetupEvent>) -> Self {
168+
pub fn new_with_provider_sender(
169+
provider_setup_tx: mpsc::Sender<crate::ProviderSetupEvent>,
170+
agent_tx: mpsc::Sender<crate::Agent>,
171+
) -> Self {
167172
let (event_tx, _) = broadcast::channel(512);
168173
Self {
169174
started_at: Instant::now(),
@@ -190,6 +195,7 @@ impl ApiState {
190195
embedding_model: RwLock::new(None),
191196
prompt_engine: RwLock::new(None),
192197
defaults_config: RwLock::new(None),
198+
agent_tx,
193199
}
194200
}
195201

src/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,11 @@ async fn run(
478478

479479
// Create the provider setup channel so API handlers can signal the main loop
480480
let (provider_tx, mut provider_rx) = mpsc::channel::<spacebot::ProviderSetupEvent>(1);
481+
// Channel for newly created agents to be registered in the main event loop
482+
let (agent_tx, mut agent_rx) = mpsc::channel::<spacebot::Agent>(8);
481483

482484
// Start HTTP API server if enabled
483-
let api_state = Arc::new(spacebot::api::ApiState::new_with_provider_sender(provider_tx));
485+
let api_state = Arc::new(spacebot::api::ApiState::new_with_provider_sender(provider_tx, agent_tx));
484486

485487
// Start background update checker
486488
spacebot::update::spawn_update_checker(api_state.update_status.clone());
@@ -842,6 +844,10 @@ async fn run(
842844
}
843845
}
844846
}
847+
Some(agent) = agent_rx.recv() => {
848+
tracing::info!(agent_id = %agent.id, "registering new agent in main loop");
849+
agents.insert(agent.id.clone(), agent);
850+
}
845851
Some(_event) = provider_rx.recv(), if !agents_initialized => {
846852
tracing::info!("provider keys configured, initializing agents");
847853

0 commit comments

Comments
 (0)