use std::borrow::Cow;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use aither_core::llm::{
AgentIdentity, CallIdentity, Event, LLMRequest, LanguageModel, Tool, ToolCall, ToolCallId,
ToolContext, ToolResult, model::Profile,
};
use futures_core::Stream;
use schemars::JsonSchema;
use serde::Deserialize;
use crate::{AgentBuilder, RunOutcome, SubagentRuns};
#[derive(Debug)]
struct NeverError;
impl std::fmt::Display for NeverError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("never")
}
}
impl std::error::Error for NeverError {}
impl aither_core::llm::Classify for NeverError {
fn failure(&self) -> aither_core::llm::Failure {
aither_core::llm::Failure::permanent()
}
}
#[derive(Clone)]
struct ScriptedLlm(Arc<Mutex<VecDeque<Vec<Event>>>>);
impl ScriptedLlm {
fn new(script: impl IntoIterator<Item = Vec<Event>>) -> Self {
Self(Arc::new(Mutex::new(script.into_iter().collect())))
}
}
impl LanguageModel for ScriptedLlm {
type Error = NeverError;
fn respond(
&self,
_request: LLMRequest,
) -> impl Stream<Item = Result<Event, Self::Error>> + Send {
let events = self
.0
.lock()
.unwrap()
.pop_front()
.expect("the script has a response for every request");
futures_lite::stream::iter(events.into_iter().map(Ok))
}
fn profile(&self) -> impl std::future::Future<Output = Result<Profile, Self::Error>> + Send {
std::future::ready(Ok(Profile::new(
"scripted",
"test",
"scripted",
"scripted model",
128_000,
)))
}
}
#[derive(Debug, Deserialize, JsonSchema)]
struct ProbeArgs {}
#[derive(Clone, Default)]
struct IdentityRecorder(Arc<Mutex<Vec<Option<CallIdentity>>>>);
impl Tool for IdentityRecorder {
fn name(&self) -> Cow<'static, str> {
"probe".into()
}
fn description(&self) -> Cow<'static, str> {
"Records the identity of its caller.".into()
}
type Arguments = ProbeArgs;
type Res = ToolResult;
fn call(
&self,
_arguments: Self::Arguments,
cx: ToolContext,
) -> impl std::future::Future<Output = aither_core::Result<Self::Res>> + Send {
self.0.lock().unwrap().push(cx.identity().cloned());
std::future::ready(Ok(ToolResult::text("probed")))
}
}
fn probe_call(id: &str) -> Event {
Event::ToolCall(ToolCall::new(id, "probe", serde_json::json!({})))
}
#[tokio::test]
async fn the_main_agent_dispatches_each_call_with_its_own_id() {
let llm = ScriptedLlm::new([
vec![probe_call("call_a"), probe_call("call_b")],
vec![Event::Text("done".to_string())],
]);
let recorder = IdentityRecorder::default();
let mut agent = AgentBuilder::new(llm).tool(recorder.clone()).build();
agent.query("probe twice").await.expect("main run");
let mut seen = recorder.0.lock().unwrap().clone();
seen.sort_by_key(|identity| {
identity
.as_ref()
.map(|identity| identity.call().to_string())
});
assert_eq!(
seen,
[
Some(CallIdentity::new(
ToolCallId::new("call_a"),
AgentIdentity::Main
)),
Some(CallIdentity::new(
ToolCallId::new("call_b"),
AgentIdentity::Main
)),
]
);
}
#[tokio::test]
async fn a_subagent_dispatches_its_calls_as_that_subagent_started_by_the_calling_call() {
let llm = ScriptedLlm::new([
vec![probe_call("child_call")],
vec![Event::Text("done".to_string())],
]);
let recorder = IdentityRecorder::default();
let parent = CallIdentity::new(ToolCallId::new("parent_call"), AgentIdentity::Main);
let runs = SubagentRuns::default();
let run = runs.start(&parent, "worker", "probe once");
let mut child = AgentBuilder::new(llm)
.identity(run.identity())
.tool(recorder.clone())
.build();
let outcome = run.run(&mut child, "probe once").await;
assert!(
matches!(outcome, RunOutcome::Completed { .. }),
"{outcome:?}"
);
let seen = recorder.0.lock().unwrap().clone();
let [Some(identity)] = seen.as_slice() else {
panic!("the subagent made one identified call, got {seen:?}");
};
assert_eq!(identity.call().as_str(), "child_call");
let AgentIdentity::Subagent(subagent) = identity.agent() else {
panic!("the call is made by a subagent, got {identity:?}");
};
assert_eq!(subagent.name(), "worker");
assert_eq!(subagent.started_by(), Some(&parent));
}