Skip to content

Commit 8c71acf

Browse files
committed
refactor a bit to send filters instead of agents
1 parent 692499d commit 8c71acf

5 files changed

Lines changed: 34 additions & 16 deletions

File tree

crates/brightstaff/src/handlers/agent_selector.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ mod tests {
197197
filter_chain: None,
198198
port: 8080,
199199
router: None,
200+
filter_agents: None,
200201
}
201202
}
202203

crates/brightstaff/src/handlers/integration_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ mod tests {
7777
filter_chain: None,
7878
port: 8080,
7979
router: None,
80+
filter_agents: None,
8081
};
8182

8283
let listeners = vec![listener];

crates/brightstaff/src/handlers/llm.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bytes::Bytes;
2-
use common::configuration::{Agent, AgentFilterChain, Listener, ModelAlias, SpanAttributes};
2+
use common::configuration::{AgentFilterChain, Listener, ModelAlias, SpanAttributes};
33
use common::consts::{
44
ARCH_IS_STREAMING_HEADER, ARCH_PROVIDER_HINT_HEADER, REQUEST_ID_HEADER, TRACE_PARENT_HEADER,
55
};
@@ -46,7 +46,6 @@ pub async fn llm_chat(
4646
span_attributes: Arc<Option<SpanAttributes>>,
4747
state_storage: Option<Arc<dyn StateStorage>>,
4848
listeners: Arc<RwLock<Vec<Listener>>>,
49-
agents_list: Arc<RwLock<Option<Vec<Agent>>>>,
5049
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
5150
let request_path = request.uri().path().to_string();
5251
let request_headers = request.headers().clone();
@@ -87,7 +86,6 @@ pub async fn llm_chat(
8786
request_path,
8887
request_headers,
8988
listeners,
90-
agents_list,
9189
)
9290
.instrument(request_span)
9391
.await
@@ -106,7 +104,6 @@ async fn llm_chat_inner(
106104
request_path: String,
107105
mut request_headers: hyper::HeaderMap,
108106
listeners: Arc<RwLock<Vec<Listener>>>,
109-
agents_list: Arc<RwLock<Option<Vec<Agent>>>>,
110107
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
111108
// Set service name for LLM operations
112109
set_service_name(operation_component::LLM);
@@ -264,22 +261,19 @@ async fn llm_chat_inner(
264261
// Check if any model listener (no agents) has a filter_chain configured
265262
{
266263
let listeners_guard = listeners.read().await;
267-
let filter_chain: Option<Vec<String>> = listeners_guard
264+
let model_listener = listeners_guard
268265
.iter()
269-
.find(|l| l.agents.is_none() && l.filter_chain.is_some())
270-
.and_then(|l| l.filter_chain.clone());
266+
.find(|l| l.agents.is_none() && l.filter_chain.is_some());
267+
268+
let filter_chain = model_listener.and_then(|l| l.filter_chain.clone());
269+
let agent_map = model_listener
270+
.and_then(|l| l.filter_agents.clone())
271+
.unwrap_or_default();
271272

272273
if let Some(ref fc) = filter_chain {
273274
if !fc.is_empty() {
274275
debug!(filter_chain = ?fc, "processing model listener filter chain");
275276

276-
// Build agent map from agents_list
277-
let agents_guard = agents_list.read().await;
278-
let agent_map: HashMap<String, Agent> = agents_guard
279-
.as_ref()
280-
.map(|agents| agents.iter().map(|a| (a.id.clone(), a.clone())).collect())
281-
.unwrap_or_default();
282-
283277
// Create a temporary AgentFilterChain to reuse PipelineProcessor
284278
let temp_filter_chain = AgentFilterChain {
285279
id: "model_listener".to_string(),

crates/brightstaff/src/main.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use hyper_util::rt::TokioIo;
2424
use opentelemetry::trace::FutureExt;
2525
use opentelemetry::{global, Context};
2626
use opentelemetry_http::HeaderExtractor;
27+
use std::collections::HashMap;
2728
use std::sync::Arc;
2829
use std::{env, fs};
2930
use tokio::net::TcpListener;
@@ -80,12 +81,32 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
8081
.cloned()
8182
.collect();
8283

84+
// Build global agent map for resolving filter chain references
85+
let global_agent_map: HashMap<String, Agent> = all_agents
86+
.iter()
87+
.map(|a| (a.id.clone(), a.clone()))
88+
.collect();
89+
90+
// Resolve filter_agents on each listener at startup
91+
let mut listeners_resolved = plano_config.listeners.clone();
92+
for listener in &mut listeners_resolved {
93+
if let Some(ref fc) = listener.filter_chain {
94+
let filter_agents: HashMap<String, Agent> = fc
95+
.iter()
96+
.filter_map(|id| global_agent_map.get(id).map(|a| (id.clone(), a.clone())))
97+
.collect();
98+
if !filter_agents.is_empty() {
99+
listener.filter_agents = Some(filter_agents);
100+
}
101+
}
102+
}
103+
83104
// Create expanded provider list for /v1/models endpoint
84105
let llm_providers = LlmProviders::try_from(plano_config.model_providers.clone())
85106
.expect("Failed to create LlmProviders");
86107
let llm_providers = Arc::new(RwLock::new(llm_providers));
87108
let combined_agents_filters_list = Arc::new(RwLock::new(Some(all_agents)));
88-
let listeners = Arc::new(RwLock::new(plano_config.listeners.clone()));
109+
let listeners = Arc::new(RwLock::new(listeners_resolved));
89110
let llm_provider_url =
90111
env::var("LLM_PROVIDER_ENDPOINT").unwrap_or_else(|_| "http://localhost:12001".to_string());
91112

@@ -249,7 +270,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
249270
span_attributes,
250271
state_storage,
251272
listeners,
252-
agents_list,
253273
)
254274
.with_context(parent_cx)
255275
.await

crates/common/src/configuration.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ pub struct Listener {
4343
pub agents: Option<Vec<AgentFilterChain>>,
4444
pub filter_chain: Option<Vec<String>>,
4545
pub port: u16,
46+
#[serde(skip)]
47+
pub filter_agents: Option<HashMap<String, Agent>>,
4648
}
4749

4850
#[derive(Debug, Clone, Serialize, Deserialize)]

0 commit comments

Comments
 (0)