Skip to main content

brainos_grpcadapter/
state.rs

1//! gRPC service-impl structs and their shared constructor/principal-resolution
2//! glue. The actual `tonic::async_trait` impls live in `handlers::memory` and
3//! `handlers::agent`.
4
5use std::sync::Arc;
6
7use tonic::Request;
8
9use crate::auth::resolve_principal_from_metadata;
10
11/// gRPC implementation of `MemoryService`.
12pub struct MemoryServiceImpl {
13    pub(crate) processor: Arc<signal::SignalProcessor>,
14    pub(crate) api_keys: Arc<Vec<brain::ApiKeyConfig>>,
15}
16
17impl MemoryServiceImpl {
18    pub fn new(processor: Arc<signal::SignalProcessor>) -> Self {
19        let api_keys = Arc::new(processor.config().access.api_keys.clone());
20        Self {
21            processor,
22            api_keys,
23        }
24    }
25
26    /// Resolve the `Principal` from the request metadata. Returns `None`
27    /// for keys without `agent_id` (back-compat).
28    pub(crate) async fn resolve_principal<T>(
29        &self,
30        req: &Request<T>,
31    ) -> Option<identity::Principal> {
32        resolve_principal_from_metadata(req, &self.api_keys, self.processor.as_ref()).await
33    }
34}
35
36/// gRPC implementation of `AgentService`.
37pub struct AgentServiceImpl {
38    pub(crate) processor: Arc<signal::SignalProcessor>,
39    pub(crate) api_keys: Arc<Vec<brain::ApiKeyConfig>>,
40}
41
42impl AgentServiceImpl {
43    pub fn new(processor: Arc<signal::SignalProcessor>) -> Self {
44        let api_keys = Arc::new(processor.config().access.api_keys.clone());
45        Self {
46            processor,
47            api_keys,
48        }
49    }
50
51    pub(crate) async fn resolve_principal<T>(
52        &self,
53        req: &Request<T>,
54    ) -> Option<identity::Principal> {
55        resolve_principal_from_metadata(req, &self.api_keys, self.processor.as_ref()).await
56    }
57}