use async_trait::async_trait;
use tokio::sync::OnceCell;
use trusty_common::inference::{
ChatRequest, ChatResponse, ChatStream, InferenceAdapter, InferenceError, ProviderCapabilities,
ProviderId, capabilities,
};
use super::bedrock::BedrockChatClient;
use super::client::OpenAiCompatClient;
const BEDROCK_PROVIDER_NAME: &str = "bedrock";
#[derive(Debug)]
pub struct DispatchingLlmClient {
openai_compat: OpenAiCompatClient,
bedrock: OnceCell<BedrockChatClient>,
}
impl DispatchingLlmClient {
pub fn new() -> Self {
Self {
openai_compat: OpenAiCompatClient::new(),
bedrock: OnceCell::new(),
}
}
async fn bedrock(&self) -> Result<&BedrockChatClient, InferenceError> {
self.bedrock
.get_or_try_init(BedrockChatClient::from_env)
.await
}
fn routes_to_bedrock(model: &str) -> bool {
crate::provider::provider_for(model).name() == BEDROCK_PROVIDER_NAME
}
}
impl Default for DispatchingLlmClient {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl InferenceAdapter for DispatchingLlmClient {
fn name(&self) -> &str {
"dispatching"
}
fn capabilities(&self) -> &ProviderCapabilities {
capabilities(ProviderId::OpenRouter)
}
fn capabilities_for(&self, model: &str) -> &ProviderCapabilities {
if Self::routes_to_bedrock(model) {
capabilities(ProviderId::Bedrock)
} else {
self.openai_compat.capabilities_for(model)
}
}
async fn chat(&self, req: &ChatRequest) -> Result<ChatResponse, InferenceError> {
if Self::routes_to_bedrock(&req.model) {
self.bedrock().await?.chat(req).await
} else {
self.openai_compat.chat(req).await
}
}
async fn chat_stream(&self, req: &ChatRequest) -> Result<ChatStream, InferenceError> {
if Self::routes_to_bedrock(&req.model) {
self.bedrock().await?.chat_stream(req).await
} else {
self.openai_compat.chat_stream(req).await
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn routes_bedrock_prefix_true() {
assert!(DispatchingLlmClient::routes_to_bedrock(
"bedrock/us.anthropic.claude-sonnet-4-6"
));
}
#[test]
fn routes_non_bedrock_prefix_false() {
for slug in [
"anthropic/claude-sonnet-4-5",
"openai/gpt-4o-mini",
"qwen/qwen-2.5-coder-32b-instruct",
"fireworks/accounts/fireworks/models/llama-v3p1-70b-instruct",
"together/meta-llama/Llama-3.3-70B-Instruct-Turbo",
] {
assert!(
!DispatchingLlmClient::routes_to_bedrock(slug),
"slug {slug} must not route to Bedrock"
);
}
}
#[test]
fn construction_touches_no_credentials() {
let client = DispatchingLlmClient::new();
assert!(
client.bedrock.get().is_none(),
"Bedrock cell must be lazy — untouched at construction"
);
}
#[tokio::test]
async fn bedrock_construction_is_lazy() {
let client = DispatchingLlmClient::new();
let req = ChatRequest {
model: "openai/gpt-4o-mini".into(),
messages: vec![],
temperature: None,
max_tokens: None,
tools: None,
tool_choice: None,
usage: None,
response_schema: None,
stop: None,
};
if !std::env::var("OPENROUTER_API_KEY").is_ok_and(|v| !v.is_empty()) {
let _ = client.chat(&req).await;
}
assert!(
client.bedrock.get().is_none(),
"a non-bedrock dispatch must never initialise the Bedrock cell"
);
}
#[test]
fn name_is_dispatching() {
assert_eq!(DispatchingLlmClient::new().name(), "dispatching");
}
#[test]
fn capabilities_report_openrouter_default() {
assert_eq!(
DispatchingLlmClient::new().capabilities().id,
ProviderId::OpenRouter
);
}
#[test]
fn capabilities_for_follows_slug_routing() {
let client = DispatchingLlmClient::new();
for (slug, expected) in [
(
"bedrock/us.anthropic.claude-sonnet-4-6",
ProviderId::Bedrock,
),
("anthropic/claude-sonnet-4-5", ProviderId::OpenRouter),
(
"fireworks/accounts/fireworks/models/llama-v3p1-70b-instruct",
ProviderId::Fireworks,
),
(
"together/meta-llama/Llama-3.3-70B-Instruct-Turbo",
ProviderId::Together,
),
] {
let caps = client.capabilities_for(slug);
assert_eq!(caps.id, expected, "capabilities for {slug} must route");
assert_eq!(
caps.id == ProviderId::Bedrock,
DispatchingLlmClient::routes_to_bedrock(slug),
"capabilities and transport routing must agree for {slug}"
);
}
let bedrock = client.capabilities_for("bedrock/us.anthropic.claude-sonnet-4-6");
assert_eq!(
bedrock.tool_dialect,
trusty_common::inference::registry::ToolDialect::AnthropicMessages
);
assert!(!bedrock.detailed_usage_accounting);
assert!(
client.bedrock.get().is_none(),
"a capability lookup must not build the Bedrock transport"
);
}
#[test]
fn stream_routes_like_chat() {
for (slug, expect_bedrock) in [
("bedrock/us.anthropic.claude-sonnet-4-6", true),
("openai/gpt-4o-mini", false),
("fireworks/accounts/fireworks/models/x", false),
] {
assert_eq!(
DispatchingLlmClient::routes_to_bedrock(slug),
expect_bedrock,
"slug {slug} must route identically for chat and chat_stream"
);
}
}
}