use {
anyhow::Result as AnyResult,
nexus_sdk::ToolFqn,
reqwest::Url,
schemars::JsonSchema,
serde::{de::DeserializeOwned, Serialize},
serde_json::{json, Value},
std::{future::Future, time::Duration},
warp::http::StatusCode,
};
pub type AuthContext = nexus_sdk::signed_http::v3::wire::AuthenticatedRequest;
pub trait NexusTool: Send + Sync + 'static {
type Input: JsonSchema + DeserializeOwned + Send;
type Output: JsonSchema + Serialize + Send;
fn fqn() -> ToolFqn;
fn timeout() -> Duration {
Duration::from_secs(10)
}
fn invoke(&self, input: Self::Input) -> impl Future<Output = Self::Output> + Send;
fn authorize(&self, _ctx: AuthContext) -> impl Future<Output = AnyResult<()>> + Send {
async { Ok(()) }
}
fn health(&self) -> impl Future<Output = AnyResult<StatusCode>> + Send;
fn path() -> &'static str {
""
}
fn description() -> &'static str;
fn new() -> impl Future<Output = Self> + Send;
fn meta(url: Url) -> Value {
let fqn = Self::fqn();
let url = url.to_string();
let timeout = Self::timeout().as_millis() as u64;
let description = Self::description();
let input_schema = schemars::schema_for!(Self::Input);
let output_schema = schemars::schema_for!(Self::Output);
json!(
{
"fqn": fqn,
"url": url,
"timeout": timeout,
"description": description,
"input_schema": input_schema,
"output_schema": output_schema,
}
)
}
}