use std::future::Future;
use crate::tenant::VerifiedCaller;
#[derive(Clone, PartialEq, Eq)]
pub struct ToolCallFrame {
session_id: Option<String>,
call_id: String,
caller: VerifiedCaller,
}
impl ToolCallFrame {
#[must_use]
pub fn new(session_id: Option<&str>, call_id: &str) -> Self {
Self {
session_id: session_id.filter(|s| !s.is_empty()).map(str::to_string),
call_id: call_id.to_string(),
caller: VerifiedCaller::default(),
}
}
#[must_use]
pub fn with_caller(mut self, caller: VerifiedCaller) -> Self {
self.caller = caller;
self
}
#[must_use]
pub fn session_id(&self) -> Option<&str> {
self.session_id.as_deref()
}
#[must_use]
pub fn call_id(&self) -> &str {
&self.call_id
}
#[must_use]
pub fn caller(&self) -> &VerifiedCaller {
&self.caller
}
}
impl std::fmt::Debug for ToolCallFrame {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ToolCallFrame")
.field("call_id", &self.call_id)
.field("has_session", &self.session_id.is_some())
.field("verified_caller", &self.caller.user_verified)
.finish()
}
}
tokio::task_local! {
static CURRENT: ToolCallFrame;
}
pub async fn within<F: Future>(frame: ToolCallFrame, fut: F) -> F::Output {
CURRENT.scope(frame, fut).await
}
#[must_use]
pub fn current_tool_call() -> Option<ToolCallFrame> {
CURRENT.try_with(Clone::clone).ok()
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[tokio::test]
async fn there_is_no_frame_outside_a_tool_call() {
assert!(current_tool_call().is_none());
}
#[tokio::test]
async fn a_frame_is_visible_inside_shadowable_and_gone_after() {
let (outer, nested) = within(ToolCallFrame::new(Some("s"), "c1"), async {
let outer = current_tool_call();
let nested = within(ToolCallFrame::new(Some("s2"), "c2"), async {
current_tool_call()
})
.await;
(outer, nested)
})
.await;
assert_eq!(
outer,
Some(ToolCallFrame {
session_id: Some("s".into()),
call_id: "c1".into(),
caller: VerifiedCaller::default(),
})
);
assert_eq!(nested.map(|f| f.call_id), Some("c2".to_string()));
assert!(current_tool_call().is_none());
}
#[test]
fn the_frame_exposes_the_caller_it_was_built_with_and_defaults_anonymous() {
let alice = VerifiedCaller {
user_verified: true,
sub: Some("alice".into()),
..VerifiedCaller::default()
};
let f = ToolCallFrame::new(Some("s"), "c").with_caller(alice.clone());
assert_eq!(f.caller(), &alice);
let anon = ToolCallFrame::new(Some("s"), "c");
assert_eq!(anon.caller(), &VerifiedCaller::default());
assert!(!anon.caller().user_verified);
}
#[test]
fn debug_prints_neither_the_session_nor_the_callers_identity() {
let f = ToolCallFrame::new(Some("secret-session"), "c7").with_caller(VerifiedCaller {
user_verified: true,
sub: Some("alice@example.com".into()),
..VerifiedCaller::default()
});
let shown = format!("{f:?}");
assert!(!shown.contains("secret-session"), "{shown}");
assert!(!shown.contains("alice"), "{shown}");
assert!(shown.contains("c7"), "{shown}");
assert!(shown.contains("has_session: true"), "{shown}");
assert!(shown.contains("verified_caller: true"), "{shown}");
}
#[test]
fn an_empty_session_is_no_session() {
assert_eq!(ToolCallFrame::new(Some(""), "c").session_id, None);
}
}