use std::fmt;
use std::sync::{Arc, OnceLock};
use std::time::Duration;
use async_trait::async_trait;
use aws_config::meta::region::ProvideRegion;
use aws_config::profile::ProfileFileRegionProvider;
use aws_config::retry::RetryConfig;
use aws_config::timeout::TimeoutConfig;
use aws_config::{BehaviorVersion, Region, SdkConfig};
use aws_credential_types::provider::error::CredentialsError;
use aws_sdk_bedrockagentcore::Client;
use aws_sdk_bedrockagentcore::config::http::HttpResponse;
use aws_sdk_bedrockagentcore::error::{DisplayErrorContext, ProvideErrorMetadata, SdkError};
use aws_sdk_bedrockagentcore::operation::invoke_web_search::builders::InvokeWebSearchFluentBuilder;
use aws_sdk_bedrockagentcore::operation::invoke_web_search::{InvokeWebSearchError, InvokeWebSearchOutput};
use futures::future::BoxFuture;
use strands::tools::FunctionTool;
use tokio::sync::OnceCell;
use super::{SNIPPET_CHARS, SearchBackend, SearchResult, WebSearchError, one_line, search_tool};
use crate::env::Env;
const WEB_SEARCH_ID: &str = "web-search.v1";
const DEFAULT_REGION: &str = "us-west-2";
const TIMEOUT: Duration = Duration::from_secs(30);
const MAX_ATTEMPTS: u32 = 2;
const ANSWER_WITHOUT_SEARCH: &str = " web_search is unavailable here; answer without it.";
#[derive(Debug, Clone, Default, bon::Builder)]
#[builder(on(String, into))]
pub struct MakeAgentCoreWebSearchOptions {
pub region: Option<String>,
pub sdk_config: Option<SdkConfig>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum CallFailure {
NotInRegion(String),
AccessDenied(String),
Credentials(String),
Other(String),
}
#[async_trait]
pub(crate) trait Invoke: Send + Sync {
async fn invoke(&self, query: &str, max_results: usize) -> Result<Vec<SearchResult>, CallFailure>;
}
#[derive(Clone)]
pub(crate) struct Connection {
pub(crate) invoke: Arc<dyn Invoke>,
pub(crate) region: String,
}
type Connect = Arc<dyn Fn() -> BoxFuture<'static, Connection> + Send + Sync>;
pub struct AgentCoreBackend {
connect: Connect,
connection: OnceCell<Connection>,
unavailable: OnceLock<String>,
}
impl fmt::Debug for AgentCoreBackend {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AgentCoreBackend")
.field(
"region",
&self.connection.get().map(|connection| connection.region.as_str()),
)
.field("unavailable", &self.unavailable.get())
.finish_non_exhaustive()
}
}
impl Default for AgentCoreBackend {
fn default() -> Self {
Self::new(MakeAgentCoreWebSearchOptions::default())
}
}
impl AgentCoreBackend {
#[must_use]
pub fn new(options: MakeAgentCoreWebSearchOptions) -> Self {
Self::with_env(options, Env::process())
}
pub(crate) fn with_env(options: MakeAgentCoreWebSearchOptions, env: Env) -> Self {
let options = Arc::new(options);
Self::with_connect(Arc::new(move || {
let options = Arc::clone(&options);
let env = env.clone();
Box::pin(async move {
let region = resolve_region(&options, &env, profile_region()).await;
let client = client_for(&options, ®ion).await;
Connection {
invoke: Arc::new(client),
region,
}
})
}))
}
pub(crate) fn with_connect(connect: Connect) -> Self {
Self {
connect,
connection: OnceCell::new(),
unavailable: OnceLock::new(),
}
}
fn mark_unavailable(&self, reason: &str) -> WebSearchError {
let message = self.unavailable.get_or_init(|| {
tracing::warn!("web_search: AgentCore web search is unavailable, so every search will fail: {reason}");
format!("{reason}{ANSWER_WITHOUT_SEARCH}")
});
WebSearchError::new(message.clone())
}
}
#[async_trait]
impl SearchBackend for AgentCoreBackend {
async fn search(&self, query: &str, max_results: usize) -> Result<Vec<SearchResult>, WebSearchError> {
if let Some(message) = self.unavailable.get() {
return Err(WebSearchError::new(message.clone()));
}
let connection = self.connection.get_or_init(|| (self.connect)()).await;
let region = &connection.region;
match connection.invoke.invoke(query, max_results).await {
Ok(results) => Ok(results),
Err(CallFailure::NotInRegion(reason)) => Err(self.mark_unavailable(&format!(
"AgentCore web search is not available in {region} ({reason})."
))),
Err(CallFailure::AccessDenied(reason)) => Err(self.mark_unavailable(&format!(
"{reason} (the caller needs bedrock-agentcore:InvokeWebSearch on arn:aws:bedrock-agentcore:{region}:aws:tool/{WEB_SEARCH_ID})."
))),
Err(CallFailure::Credentials(reason)) => Err(self.mark_unavailable(&format!(
"{reason} (AgentCore web search is signed with AWS credentials; a Bedrock API key does not cover it)."
))),
Err(CallFailure::Other(reason)) => Err(WebSearchError::new(reason)),
}
}
}
#[must_use]
pub fn make_agent_core_web_search(options: MakeAgentCoreWebSearchOptions) -> FunctionTool {
search_tool(Arc::new(AgentCoreBackend::new(options)))
}
pub(crate) fn agent_core_web_search(env: &Env) -> FunctionTool {
search_tool(Arc::new(AgentCoreBackend::with_env(
MakeAgentCoreWebSearchOptions::default(),
env.clone(),
)))
}
fn profile_region() -> BoxFuture<'static, Option<String>> {
Box::pin(async {
ProfileFileRegionProvider::new()
.region()
.await
.map(|region| region.to_string())
})
}
pub(crate) async fn resolve_region(
options: &MakeAgentCoreWebSearchOptions,
env: &Env,
profile: BoxFuture<'static, Option<String>>,
) -> String {
let configured = options
.region
.clone()
.filter(|region| !region.is_empty())
.or_else(|| {
options
.sdk_config
.as_ref()
.and_then(SdkConfig::region)
.map(ToString::to_string)
})
.or_else(|| env.get("AWS_REGION"))
.or_else(|| env.get("AWS_DEFAULT_REGION"));
if let Some(region) = configured {
return region;
}
profile.await.unwrap_or_else(|| DEFAULT_REGION.to_owned())
}
pub(crate) async fn client_for(options: &MakeAgentCoreWebSearchOptions, region: &str) -> Client {
let sdk_config = match &options.sdk_config {
Some(config) => config.clone(),
None => {
aws_config::defaults(BehaviorVersion::latest())
.region(Region::new(region.to_owned()))
.load()
.await
}
};
let timeouts = sdk_config
.timeout_config()
.map(TimeoutConfig::to_builder)
.unwrap_or_default()
.operation_timeout(TIMEOUT)
.build();
let config = aws_sdk_bedrockagentcore::config::Builder::from(&sdk_config)
.region(Region::new(region.to_owned()))
.retry_config(RetryConfig::standard().with_max_attempts(MAX_ATTEMPTS))
.timeout_config(timeouts)
.build();
Client::from_conf(config)
}
#[async_trait]
impl Invoke for Client {
async fn invoke(&self, query: &str, max_results: usize) -> Result<Vec<SearchResult>, CallFailure> {
let output = request(self, query, max_results)
.send()
.await
.map_err(|error| classify(&error))?;
Ok(results(&output))
}
}
#[allow(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
reason = "max_results is clamped to 1..=10"
)]
pub(crate) fn request(client: &Client, query: &str, max_results: usize) -> InvokeWebSearchFluentBuilder {
client
.invoke_web_search()
.web_search_id(WEB_SEARCH_ID)
.search_query(query)
.max_results(max_results as i32)
}
pub(crate) fn results(output: &InvokeWebSearchOutput) -> Vec<SearchResult> {
output
.results()
.iter()
.map(|result| {
let url = result.url().unwrap_or_default();
let title = result.title().filter(|title| !title.is_empty()).unwrap_or(url);
let snippet: String = one_line(result.text()).chars().take(SNIPPET_CHARS).collect();
SearchResult::new(title, url, snippet)
})
.collect()
}
pub(crate) fn classify(error: &SdkError<InvokeWebSearchError, HttpResponse>) -> CallFailure {
let service = error.as_service_error();
let reason = service
.and_then(service_message)
.map_or_else(|| DisplayErrorContext(error).to_string(), ToOwned::to_owned);
let not_found = service.is_some_and(InvokeWebSearchError::is_resource_not_found_exception);
if error.raw_response().is_some_and(|raw| raw.status().as_u16() == 404) && !not_found {
return CallFailure::NotInRegion(reason);
}
if service.is_some_and(InvokeWebSearchError::is_access_denied_exception) {
return CallFailure::AccessDenied(reason);
}
if let Some(credentials) = credentials_failure(error) {
return CallFailure::Credentials(credentials);
}
CallFailure::Other(reason)
}
fn service_message(error: &InvokeWebSearchError) -> Option<&str> {
let message = match error {
InvokeWebSearchError::AccessDeniedException(exception) => exception.message(),
InvokeWebSearchError::InternalServerException(exception) => exception.message(),
InvokeWebSearchError::ResourceNotFoundException(exception) => exception.message(),
InvokeWebSearchError::ServiceUnavailableException(exception) => Some(exception.message()),
InvokeWebSearchError::ThrottlingException(exception) => exception.message(),
InvokeWebSearchError::ValidationException(exception) => Some(exception.message()),
_ => None,
};
message.or_else(|| error.message())
}
fn credentials_failure(error: &(dyn std::error::Error + 'static)) -> Option<String> {
let mut current = Some(error);
while let Some(error) = current {
if let Some(credentials) = error.downcast_ref::<CredentialsError>() {
let text = DisplayErrorContext(credentials).to_string();
let first = text.lines().next().unwrap_or_default();
return Some(first.trim_end_matches(" Attempted:").to_owned());
}
current = error.source();
}
None
}
#[cfg(test)]
mod tests;