use crate::convenience::{
clear_a2a_context, clear_llm_context, clear_request_context, set_a2a_context, set_llm_context,
set_request_context,
};
use std::sync::{Arc, OnceLock};
pub trait LlmContextManager: Send + Sync + std::fmt::Debug {
fn set_context(&self, model: &str, component: &str);
fn clear_context(&self);
}
pub trait A2aContextManager: Send + Sync + std::fmt::Debug {
fn set_context(&self, message_type: &str, from_agent: &str, to_agent: &str, component: &str);
fn clear_context(&self);
}
pub trait RequestContextManager: Send + Sync + std::fmt::Debug {
fn set_context(&self, request_id: &str, user_id: Option<&str>, session_id: Option<&str>);
fn clear_context(&self);
}
pub trait ContextManagerRegistry: Send + Sync + std::fmt::Debug {
fn get_llm_manager(&self) -> Option<Arc<dyn LlmContextManager>>;
fn get_a2a_manager(&self) -> Option<Arc<dyn A2aContextManager>>;
fn get_request_manager(&self) -> Option<Arc<dyn RequestContextManager>>;
}
static CONTEXT_REGISTRY: OnceLock<Arc<dyn ContextManagerRegistry>> = OnceLock::new();
pub fn register_context_managers(registry: Arc<dyn ContextManagerRegistry>) {
CONTEXT_REGISTRY.set(registry).ok();
}
fn get_context_registry() -> Option<Arc<dyn ContextManagerRegistry>> {
CONTEXT_REGISTRY.get().cloned()
}
#[derive(Debug)]
pub struct StructuredLlmContextManager;
impl LlmContextManager for StructuredLlmContextManager {
fn set_context(&self, model: &str, component: &str) {
set_llm_context(model, component);
}
fn clear_context(&self) {
clear_llm_context();
}
}
#[derive(Debug)]
pub struct StructuredA2aContextManager;
impl A2aContextManager for StructuredA2aContextManager {
fn set_context(&self, message_type: &str, from_agent: &str, to_agent: &str, component: &str) {
set_a2a_context(message_type, from_agent, to_agent, component);
}
fn clear_context(&self) {
clear_a2a_context();
}
}
#[derive(Debug)]
pub struct StructuredRequestContextManager;
impl RequestContextManager for StructuredRequestContextManager {
fn set_context(&self, request_id: &str, user_id: Option<&str>, session_id: Option<&str>) {
set_request_context(request_id, user_id, session_id);
}
fn clear_context(&self) {
clear_request_context();
}
}
#[derive(Debug)]
pub struct StructuredContextRegistry {
llm_manager: Option<Arc<dyn LlmContextManager>>,
a2a_manager: Option<Arc<dyn A2aContextManager>>,
request_manager: Option<Arc<dyn RequestContextManager>>,
}
impl StructuredContextRegistry {
pub fn new() -> Self {
Self {
llm_manager: Some(Arc::new(StructuredLlmContextManager)),
a2a_manager: Some(Arc::new(StructuredA2aContextManager)),
request_manager: Some(Arc::new(StructuredRequestContextManager)),
}
}
pub fn register(&self) {
let registry = Arc::new(Self {
llm_manager: self.llm_manager.clone(),
a2a_manager: self.a2a_manager.clone(),
request_manager: self.request_manager.clone(),
});
register_context_managers(registry);
}
}
impl Default for StructuredContextRegistry {
fn default() -> Self {
Self::new()
}
}
impl ContextManagerRegistry for StructuredContextRegistry {
fn get_llm_manager(&self) -> Option<Arc<dyn LlmContextManager>> {
self.llm_manager.clone()
}
fn get_a2a_manager(&self) -> Option<Arc<dyn A2aContextManager>> {
self.a2a_manager.clone()
}
fn get_request_manager(&self) -> Option<Arc<dyn RequestContextManager>> {
self.request_manager.clone()
}
}
pub struct LlmContextGuard {
_phantom: std::marker::PhantomData<()>,
}
impl Drop for LlmContextGuard {
fn drop(&mut self) {
if let Some(registry) = get_context_registry() {
if let Some(manager) = registry.get_llm_manager() {
manager.clear_context();
}
}
}
}
pub struct A2aContextGuard {
_phantom: std::marker::PhantomData<()>,
}
impl Drop for A2aContextGuard {
fn drop(&mut self) {
if let Some(registry) = get_context_registry() {
if let Some(manager) = registry.get_a2a_manager() {
manager.clear_context();
}
}
}
}
pub struct RequestContextGuard {
_phantom: std::marker::PhantomData<()>,
}
impl Drop for RequestContextGuard {
fn drop(&mut self) {
if let Some(registry) = get_context_registry() {
if let Some(manager) = registry.get_request_manager() {
manager.clear_context();
}
}
}
}
pub struct AllContextsGuard {
_phantom: std::marker::PhantomData<()>,
}
impl Drop for AllContextsGuard {
fn drop(&mut self) {
if let Some(registry) = get_context_registry() {
if let Some(manager) = registry.get_request_manager() {
manager.clear_context();
}
if let Some(manager) = registry.get_a2a_manager() {
manager.clear_context();
}
if let Some(manager) = registry.get_llm_manager() {
manager.clear_context();
}
}
}
}
pub fn set_llm_context_scoped(model: &str, component: &str) -> LlmContextGuard {
if let Some(registry) = get_context_registry() {
if let Some(manager) = registry.get_llm_manager() {
manager.set_context(model, component);
}
}
LlmContextGuard {
_phantom: std::marker::PhantomData,
}
}
pub fn set_a2a_context_scoped(
message_type: &str,
from_agent: &str,
to_agent: &str,
component: &str,
) -> A2aContextGuard {
if let Some(registry) = get_context_registry() {
if let Some(manager) = registry.get_a2a_manager() {
manager.set_context(message_type, from_agent, to_agent, component);
}
}
A2aContextGuard {
_phantom: std::marker::PhantomData,
}
}
pub fn set_request_context_scoped(
request_id: &str,
user_id: Option<&str>,
session_id: Option<&str>,
) -> RequestContextGuard {
if let Some(registry) = get_context_registry() {
if let Some(manager) = registry.get_request_manager() {
manager.set_context(request_id, user_id, session_id);
}
}
RequestContextGuard {
_phantom: std::marker::PhantomData,
}
}
pub fn set_all_contexts_scoped(
model: &str,
component: &str,
message_type: &str,
from_agent: &str,
to_agent: &str,
a2a_component: &str,
request_id: &str,
user_id: Option<&str>,
session_id: Option<&str>,
) -> AllContextsGuard {
if let Some(registry) = get_context_registry() {
if let Some(manager) = registry.get_llm_manager() {
manager.set_context(model, component);
}
if let Some(manager) = registry.get_a2a_manager() {
manager.set_context(message_type, from_agent, to_agent, a2a_component);
}
if let Some(manager) = registry.get_request_manager() {
manager.set_context(request_id, user_id, session_id);
}
}
AllContextsGuard {
_phantom: std::marker::PhantomData,
}
}
pub fn with_llm_context<F, R>(model: &str, component: &str, f: F) -> R
where
F: FnOnce() -> R,
{
let _guard = set_llm_context_scoped(model, component);
f()
}
pub fn with_a2a_context<F, R>(
message_type: &str,
from_agent: &str,
to_agent: &str,
component: &str,
f: F,
) -> R
where
F: FnOnce() -> R,
{
let _guard = set_a2a_context_scoped(message_type, from_agent, to_agent, component);
f()
}
pub fn with_request_context<F, R>(
request_id: &str,
user_id: Option<&str>,
session_id: Option<&str>,
f: F,
) -> R
where
F: FnOnce() -> R,
{
let _guard = set_request_context_scoped(request_id, user_id, session_id);
f()
}
pub fn with_all_contexts<F, R>(
model: &str,
component: &str,
message_type: &str,
from_agent: &str,
to_agent: &str,
a2a_component: &str,
request_id: &str,
user_id: Option<&str>,
session_id: Option<&str>,
f: F,
) -> R
where
F: FnOnce() -> R,
{
let _guard = set_all_contexts_scoped(
model,
component,
message_type,
from_agent,
to_agent,
a2a_component,
request_id,
user_id,
session_id,
);
f()
}
pub struct ScopedContextBuilder {
guards: Vec<Box<dyn std::any::Any + Send>>,
}
impl ScopedContextBuilder {
pub fn new() -> Self {
Self { guards: Vec::new() }
}
pub fn with_llm_context(mut self, model: &str, component: &str) -> Self {
let guard = set_llm_context_scoped(model, component);
self.guards.push(Box::new(guard));
self
}
pub fn with_a2a_context(
mut self,
message_type: &str,
from_agent: &str,
to_agent: &str,
component: &str,
) -> Self {
let guard = set_a2a_context_scoped(message_type, from_agent, to_agent, component);
self.guards.push(Box::new(guard));
self
}
pub fn with_request_context(
mut self,
request_id: &str,
user_id: Option<&str>,
session_id: Option<&str>,
) -> Self {
let guard = set_request_context_scoped(request_id, user_id, session_id);
self.guards.push(Box::new(guard));
self
}
pub fn execute<F, R>(&self, f: F) -> R
where
F: FnOnce() -> R,
{
f()
}
}
impl Drop for ScopedContextBuilder {
fn drop(&mut self) {
self.guards.clear();
}
}
impl Default for ScopedContextBuilder {
fn default() -> Self {
Self::new()
}
}
#[macro_export]
macro_rules! with_llm_context_scoped {
($model:expr_2021, $component:expr_2021 => $block:block) => {
$crate::context_adapter::with_llm_context($model, $component, || $block)
};
}
#[macro_export]
macro_rules! with_a2a_context_scoped {
($message_type:expr_2021, $from_agent:expr_2021, $to_agent:expr_2021, $component:expr_2021 => $block:block) => {
$crate::context_adapter::with_a2a_context(
$message_type,
$from_agent,
$to_agent,
$component,
|| $block,
)
};
}
#[macro_export]
macro_rules! with_request_context_scoped {
($request_id:expr_2021, $user_id:expr_2021, $session_id:expr_2021 => $block:block) => {
$crate::context_adapter::with_request_context($request_id, $user_id, $session_id, || $block)
};
}
pub fn init_context_integration() {
let registry = StructuredContextRegistry::new();
registry.register();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_context_integration() {
init_context_integration();
{
let _guard = set_llm_context_scoped("gpt-4", "test_component");
}
}
#[test]
fn test_scoped_callback_integration() {
init_context_integration();
let result = with_llm_context("gpt-4", "test_component", || "test_result");
assert_eq!(result, "test_result");
}
#[test]
fn test_scoped_builder() {
init_context_integration();
let builder = ScopedContextBuilder::new()
.with_llm_context("gpt-4", "test_component")
.with_request_context("req-123", Some("user-456"), None);
let result = builder.execute(|| "builder_test");
assert_eq!(result, "builder_test");
}
}