A high-performance inference engine for AI models
update endpoint client
eugene3d17d618eeaa
diff --git a/Cargo.lock b/Cargo.lockindex 431b86c5e..b2117e47e 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -520,7 +520,7 @@ dependencies = ["clang-sys","itertools 0.13.0","log",- "prettyplease",+ "prettyplease 0.2.37","proc-macro2","quote","regex",@@ -647,6 +647,29 @@ dependencies = ["objc2",]+[[package]]+name = "bon"+version = "3.10.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9e3fac94a66da67200398458a25412bcc3f9b6443b5119a6cad9cf3ccfcd8cc6"+dependencies = [+ "bon-macros",+]++[[package]]+name = "bon-macros"+version = "3.10.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d4654961ad0494e4774c5c60b4cb4cd0ae9b9d92d039d901638b1dba97ebebf5"+dependencies = [+ "darling 0.24.1",+ "ident_case",+ "prettyplease 0.3.0",+ "proc-macro2",+ "quote",+ "syn 3.0.3",+]+[[package]]name = "borrow-or-share"version = "0.2.4"@@ -1031,7 +1054,7 @@ version = "3.1.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "faf9468729b8cbcea668e36183cb69d317348c2e08e994829fb56ebfdfbaac34"dependencies = [- "windows-sys 0.59.0",+ "windows-sys 0.61.2",][[package]]@@ -1476,6 +1499,16 @@ dependencies = ["darling_macro 0.23.0",]+[[package]]+name = "darling"+version = "0.24.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ed17f5901b6630b993ca003def43f2f8ef4014fc13b047b57aad617ff32bc2ec"+dependencies = [+ "darling_core 0.24.1",+ "darling_macro 0.24.1",+]+[[package]]name = "darling_core"version = "0.20.11"@@ -1503,6 +1536,19 @@ dependencies = ["syn 2.0.118",]+[[package]]+name = "darling_core"+version = "0.24.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6837e2cf7485aaae18f86181d2f0e9a7ed297a025e220aeabf63fdebd3a2ddff"+dependencies = [+ "ident_case",+ "proc-macro2",+ "quote",+ "strsim",+ "syn 3.0.3",+]+[[package]]name = "darling_macro"version = "0.20.11"@@ -1525,6 +1571,17 @@ dependencies = ["syn 2.0.118",]+[[package]]+name = "darling_macro"+version = "0.24.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "2ac7135c3ef02b2f7833bbeb1be5ba7f966dcde8a87c6b87f65a778d71a02785"+dependencies = [+ "darling_core 0.24.1",+ "quote",+ "syn 3.0.3",+]+[[package]]name = "dary_heap"version = "0.3.9"@@ -1703,7 +1760,7 @@ dependencies = ["libc","option-ext","redox_users",- "windows-sys 0.59.0",+ "windows-sys 0.61.2",][[package]]@@ -1849,7 +1906,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"dependencies = ["libc",- "windows-sys 0.59.0",+ "windows-sys 0.61.2",][[package]]@@ -2960,7 +3017,7 @@ checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46"dependencies = ["hermit-abi","libc",- "windows-sys 0.59.0",+ "windows-sys 0.61.2",][[package]]@@ -3690,6 +3747,7 @@ dependencies = ["async-trait","bindings","bindings-types",+ "bon","chrono","futures","hanashi",@@ -3909,7 +3967,7 @@ version = "0.50.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"dependencies = [- "windows-sys 0.59.0",+ "windows-sys 0.61.2",][[package]]@@ -4590,6 +4648,16 @@ dependencies = ["syn 2.0.118",]+[[package]]+name = "prettyplease"+version = "0.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "2bfe0f4c752e450fc2faf62654f1c134747922825d5b04ca717b8874f41a40c0"+dependencies = [+ "proc-macro2",+ "syn 3.0.3",+]+[[package]]name = "proc-macro-crate"version = "3.5.0"@@ -4876,7 +4944,7 @@ dependencies = ["once_cell","socket2 0.6.4","tracing",- "windows-sys 0.59.0",+ "windows-sys 0.61.2",][[package]]@@ -5613,7 +5681,7 @@ dependencies = ["errno","libc","linux-raw-sys",- "windows-sys 0.59.0",+ "windows-sys 0.61.2",][[package]]@@ -5671,7 +5739,7 @@ dependencies = ["security-framework","security-framework-sys","webpki-root-certs",- "windows-sys 0.59.0",+ "windows-sys 0.61.2",][[package]]@@ -6517,7 +6585,7 @@ dependencies = ["getrandom 0.4.3","once_cell","rustix",- "windows-sys 0.59.0",+ "windows-sys 0.61.2",][[package]]@@ -7533,6 +7601,7 @@ dependencies = ["bindings","bindings-types","bitflags 2.13.0",+ "bon","chrono","confy","dirs",@@ -7615,7 +7684,7 @@ dependencies = ["objc2","objc2-foundation","parking_lot",- "prettyplease",+ "prettyplease 0.2.37","proc-macro2","proptest","quote",@@ -7961,7 +8030,7 @@ version = "0.1.11"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"dependencies = [- "windows-sys 0.59.0",+ "windows-sys 0.61.2",][[package]]diff --git a/Cargo.toml b/Cargo.tomlindex c94f0e49f..b4429c629 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -33,7 +33,6 @@ readme = "README.md"homepage = "https://trymirai.com"repository = "https://github.com/trymirai/uzu"license = "MIT"-license-file = "LICENSE"keywords = ["rust","ai",@@ -84,7 +83,6 @@ block2 = "0.6"bytemuck = "1.23"bytesize = "2"xxhash-rust = "0.8.15"-memmap2 = "0.9.8"num-traits = "0.2"regex = "1.12"fancy-regex = "0.17.0"@@ -129,7 +127,6 @@ rstest = "0.26"wiremock = "0.6"proptest = { version = "1.10", default-features = false, features = ["std"] }criterion = { version = "0.8.2", default-features = false }-rustix = "1.1.4"indexmap = { version = "2.13.0", features = ["serde"] }tracing = "0.1.44"tracing-subscriber = { version = "0.3.22", features = ["env-filter"] }@@ -146,11 +143,11 @@ reqwest = { version = "0.13.2", features = ["blocking","query",] }-async-shutdown = "0.2.2"objc-sys = "0.3.5"crc32c = "0.6.8"base64 = "0.22.1"bitflags = "2.11.1"+bon = "3.10.0"chrono = { version = "0.4.42", features = ["serde"] }jsonschema = "0.46.0"# Please make sure to use compatible versions of the cargo-swift / uniffi pair.diff --git a/crates/legacy/kanka/Cargo.toml b/crates/legacy/kanka/Cargo.tomlindex baea518e1..e6df21352 100644--- a/crates/legacy/kanka/Cargo.toml+++ b/crates/legacy/kanka/Cargo.toml@@ -8,7 +8,6 @@ homepage.workspace = truerepository.workspace = truelicense.workspace = truedescription = "Generic obfuscated dlsym bindings for private Apple framework APIs"-readme = "README.md"[lib]name = "kanka"diff --git a/crates/legacy/keisoku/Cargo.toml b/crates/legacy/keisoku/Cargo.tomlindex e39893315..0bb525253 100644--- a/crates/legacy/keisoku/Cargo.toml+++ b/crates/legacy/keisoku/Cargo.toml@@ -8,7 +8,6 @@ homepage.workspace = truerepository.workspace = truelicense.workspace = truedescription = "System telemetry collector (CPU/GPU/RAM/power/sensors) for Apple platforms"-readme = "README.md"[lib]name = "keisoku"diff --git a/crates/legacy/nagare/Cargo.toml b/crates/legacy/nagare/Cargo.tomlindex 7f67c5526..b361d64a0 100644--- a/crates/legacy/nagare/Cargo.toml+++ b/crates/legacy/nagare/Cargo.toml@@ -52,6 +52,7 @@ bindings-wasm = [[dependencies]bindings-types.workspace = true+bon.workspace = truebindings.workspace = truenagare-macros.workspace = trueshoji.workspace = truediff --git a/crates/legacy/nagare/src/api/client.rs b/crates/legacy/nagare/src/api/client.rsindex ceab74273..5db7643d7 100644--- a/crates/legacy/nagare/src/api/client.rs+++ b/crates/legacy/nagare/src/api/client.rs@@ -1,93 +1,80 @@-use reqwest::Client as ReqwestClient;-use serde::de::DeserializeOwned;+use std::time::Duration;-use crate::api::{Config, Endpoint, Error};+use bon::bon;+use reqwest::{Client as ReqwestClient, RequestBuilder, Response};+use serde::Serialize;++use crate::api::{Endpoint, Error, RetryConfig};pub struct Client {- config: Config,client: ReqwestClient,+ base_url: String,+ bearer_token: Option<String>,+ retry: RetryConfig,}+#[bon]impl Client {- pub fn new(config: Config) -> Result<Self, Error> {- #[allow(unused_mut)]- let mut client_builder = reqwest::Client::builder();- #[cfg(not(target_family = "wasm"))]- {- client_builder = client_builder.timeout(config.timeout);- }- let client = client_builder.build().map_err(Error::from)?;+ #[builder]+ pub fn new(+ #[builder(into)] base_url: String,+ #[builder(default = Duration::from_secs(10))] timeout: Duration,+ bearer_token: Option<String>,+ #[builder(default)] retry: RetryConfig,+ ) -> Result<Self, Error> {+ let client = ReqwestClient::builder().timeout(timeout).build().map_err(Error::from)?;Ok(Self {- config,client,+ base_url,+ bearer_token,+ retry,})}- pub async fn response<E: Endpoint, T: DeserializeOwned>(+ pub async fn call<E: Endpoint>(&self,- endpoint: &E,- ) -> Result<T, Error> {- let response = self.send_checked(endpoint).await?;- response.json::<T>().await.map_err(|error| Error::Decode(error.to_string()))+ request: &E::Request,+ ) -> Result<E::Response, Error> {+ let response = self.checked(E::PATH, request).await?;+ response.json::<E::Response>().await.map_err(|error| Error::Decode(error.to_string()))}pub async fn send<E: Endpoint>(&self,- endpoint: &E,+ request: &E::Request,) -> Result<(), Error> {- self.send_checked(endpoint).await?;+ self.checked(E::PATH, request).await?;Ok(())}}impl Client {- async fn send_checked<E: Endpoint>(+ async fn checked(&self,- endpoint: &E,- ) -> Result<reqwest::Response, Error> {- let response = self.build_request(endpoint).send().await?;+ path: &str,+ body: &impl Serialize,+ ) -> Result<Response, Error> {+ let response = self.retry.send(|| self.request(path).json(body).send()).await?;let status = response.status();if !status.is_success() {let body = response.text().await.unwrap_or_default();return Err(Error::Http {- code: status.as_u16(),+ code: status,body,});}Ok(response)}- fn build_request<E: Endpoint>(+ fn request(&self,- endpoint: &E,- ) -> reqwest::RequestBuilder {- let url = self.url(endpoint);- let method = endpoint.method();- let payload = endpoint.payload(&self.config);-- let mut request = self.client.request(method, url);- if let Some(query) = payload.query {- request = request.query(- &query.into_iter().map(|(key, value)| (key, value.to_string())).collect::<Vec<(String, String)>>(),- );- }- if let Some(body) = payload.body {- request = request.json(&body);- }- for (key, value) in self.config.headers.iter() {- request = request.header(key, value);+ path: &str,+ ) -> RequestBuilder {+ let request = self.client.post(format!("{}/{}", self.base_url, path));+ match &self.bearer_token {+ Some(token) => request.bearer_auth(token),+ None => request,}- for (key, value) in endpoint.headers().iter() {- request = request.header(key, value);- }- request- }-- fn url<E: Endpoint>(- &self,- endpoint: &E,- ) -> String {- format!("{}/{}", self.config.base_url, endpoint.path())}}diff --git a/crates/legacy/nagare/src/api/config.rs b/crates/legacy/nagare/src/api/config.rsdeleted file mode 100644index d436b6534..000000000--- a/crates/legacy/nagare/src/api/config.rs+++ /dev/null@@ -1,23 +0,0 @@-use std::time::Duration;--use indexmap::IndexMap;--pub struct Config {- pub base_url: String,- pub timeout: Duration,- pub headers: IndexMap<String, String>,-}--impl Config {- pub fn new(- base_url: String,- timeout: Duration,- headers: IndexMap<String, String>,- ) -> Self {- Self {- base_url,- timeout,- headers,- }- }-}diff --git a/crates/legacy/nagare/src/api/endpoint.rs b/crates/legacy/nagare/src/api/endpoint.rsindex e429ec042..e208152e0 100644--- a/crates/legacy/nagare/src/api/endpoint.rs+++ b/crates/legacy/nagare/src/api/endpoint.rs@@ -1,20 +1,8 @@-use indexmap::IndexMap;-use reqwest::Method;-use serde_json::Value;--use crate::api::Config;--pub struct Payload {- pub query: Option<IndexMap<String, String>>,- pub body: Option<Value>,-}+use serde::{Serialize, de::DeserializeOwned};pub trait Endpoint {- fn method(&self) -> Method;- fn path(&self) -> String;- fn headers(&self) -> IndexMap<String, String>;- fn payload(- &self,- config: &Config,- ) -> Payload;+ const PATH: &'static str;++ type Request: Serialize;+ type Response: DeserializeOwned;}diff --git a/crates/legacy/nagare/src/api/error.rs b/crates/legacy/nagare/src/api/error.rsindex 318cae9f1..6dc507600 100644--- a/crates/legacy/nagare/src/api/error.rs+++ b/crates/legacy/nagare/src/api/error.rs@@ -1,8 +1,12 @@+use reqwest::StatusCode;++use crate::api::IsTransient;+#[derive(thiserror::Error, Debug)]pub enum Error {#[error("HTTP {code}: {body}")]Http {- code: u16,+ code: StatusCode,body: String,},#[error("Timeout")]@@ -22,3 +26,16 @@ impl From<reqwest::Error> for Error {}}}++impl IsTransient for Error {+ fn is_transient(&self) -> bool {+ match self {+ Self::Timeout | Self::Network(_) => true,+ Self::Http {+ code,+ ..+ } => code.is_transient(),+ Self::Decode(_) => false,+ }+ }+}diff --git a/crates/legacy/nagare/src/api/is_transient.rs b/crates/legacy/nagare/src/api/is_transient.rsnew file mode 100644index 000000000..23873e5ca--- /dev/null+++ b/crates/legacy/nagare/src/api/is_transient.rs@@ -0,0 +1,17 @@+use reqwest::StatusCode;++pub trait IsTransient {+ fn is_transient(&self) -> bool;+}++impl IsTransient for StatusCode {+ fn is_transient(&self) -> bool {+ self.is_server_error() || matches!(*self, StatusCode::REQUEST_TIMEOUT | StatusCode::TOO_MANY_REQUESTS)+ }+}++impl IsTransient for reqwest::Error {+ fn is_transient(&self) -> bool {+ !(self.is_builder() || self.is_redirect() || self.is_decode() || self.is_status())+ }+}diff --git a/crates/legacy/nagare/src/api/mod.rs b/crates/legacy/nagare/src/api/mod.rsindex 85e0f0ed6..5e2989716 100644--- a/crates/legacy/nagare/src/api/mod.rs+++ b/crates/legacy/nagare/src/api/mod.rs@@ -1,9 +1,11 @@mod client;-mod config;mod endpoint;mod error;+mod is_transient;+mod retry_config;pub use client::Client;-pub use config::Config;-pub use endpoint::{Endpoint, Payload};+pub use endpoint::Endpoint;pub use error::Error;+pub use is_transient::IsTransient;+pub use retry_config::RetryConfig;diff --git a/crates/legacy/nagare/src/api/retry_config.rs b/crates/legacy/nagare/src/api/retry_config.rsnew file mode 100644index 000000000..c087ff818--- /dev/null+++ b/crates/legacy/nagare/src/api/retry_config.rs@@ -0,0 +1,67 @@+use std::{future::Future, time::Duration};++use reqwest::{Response, header::HeaderMap};+use tokio::time::{Instant, sleep, timeout};++use crate::api::{Error, IsTransient};++#[derive(Debug, Clone, Copy)]+pub struct RetryConfig {+ pub max_attempts: usize,+ pub base_delay: Duration,+ pub budget: Duration,+}++impl Default for RetryConfig {+ fn default() -> Self {+ Self {+ max_attempts: 4,+ base_delay: Duration::from_millis(100),+ budget: Duration::from_secs(15),+ }+ }+}++impl RetryConfig {+ pub async fn send<F, Fut>(+ &self,+ mut send_request: F,+ ) -> Result<Response, Error>+ where+ F: FnMut() -> Fut,+ Fut: Future<Output = Result<Response, reqwest::Error>>,+ {+ let attempts = self.max_attempts.max(1);+ let deadline = Instant::now() + self.budget;+ let mut delay = self.base_delay;+ let mut attempt = 0;++ loop {+ attempt += 1;+ let remaining = deadline.saturating_duration_since(Instant::now());+ if remaining.is_zero() {+ return Err(Error::Timeout);+ }+ let Ok(result) = timeout(remaining, send_request()).await else {+ return Err(Error::Timeout);+ };++ let transient = match &result {+ Ok(response) => response.status().is_transient(),+ Err(error) => error.is_transient(),+ };+ if !transient || attempt == attempts {+ return result.map_err(Error::from);+ }++ let requested = result.as_ref().ok().and_then(|response| retry_after(response.headers()));+ sleep(requested.unwrap_or(delay).min(deadline.saturating_duration_since(Instant::now()))).await;+ delay = delay.saturating_mul(2);+ }+ }+}++fn retry_after(headers: &HeaderMap) -> Option<Duration> {+ let seconds = headers.get(reqwest::header::RETRY_AFTER)?.to_str().ok()?.trim().parse().ok()?;+ Some(Duration::from_secs(seconds))+}diff --git a/crates/legacy/nagare/src/lib.rs b/crates/legacy/nagare/src/lib.rsindex 92ec15fac..9a74219af 100644--- a/crates/legacy/nagare/src/lib.rs+++ b/crates/legacy/nagare/src/lib.rs@@ -1,6 +1,7 @@#[cfg(feature = "bindings-uniffi")]uniffi::setup_scaffolding!();+#[cfg(not(target_family = "wasm"))]pub mod api;pub mod chat;pub mod classification;diff --git a/crates/legacy/nagare/src/telemetry/endpoint.rs b/crates/legacy/nagare/src/telemetry/body.rssimilarity index 74%rename from crates/legacy/nagare/src/telemetry/endpoint.rsrename to crates/legacy/nagare/src/telemetry/body.rsindex caa831102..9456282a3 100644--- a/crates/legacy/nagare/src/telemetry/endpoint.rs+++ b/crates/legacy/nagare/src/telemetry/body.rs@@ -1,53 +1,8 @@-use indexmap::IndexMap;-use reqwest::{Method, header::CONTENT_TYPE};use serde_json::Value;use super::{TelemetryContext, record::TelemetryRecord};-use crate::api::{Config, Endpoint, Payload};-pub(super) struct TelemetryEndpoint {- path: String,- body: Value,-}--impl TelemetryEndpoint {- pub(super) fn new(- path: String,- context: &TelemetryContext,- record: &TelemetryRecord,- ) -> Self {- Self {- path,- body: build_body(context, record),- }- }-}--impl Endpoint for TelemetryEndpoint {- fn method(&self) -> Method {- Method::POST- }-- fn path(&self) -> String {- self.path.clone()- }-- fn headers(&self) -> IndexMap<String, String> {- IndexMap::from([(CONTENT_TYPE.to_string(), "application/json".to_string())])- }-- fn payload(- &self,- _: &Config,- ) -> Payload {- Payload {- query: None,- body: Some(self.body.clone()),- }- }-}--fn build_body(+pub fn build_body(context: &TelemetryContext,record: &TelemetryRecord,) -> Value {diff --git a/crates/legacy/nagare/src/telemetry/events.rs b/crates/legacy/nagare/src/telemetry/events.rsnew file mode 100644index 000000000..92d81eef7--- /dev/null+++ b/crates/legacy/nagare/src/telemetry/events.rs@@ -0,0 +1,12 @@+use serde_json::Value;++use crate::api::Endpoint;++pub struct Events;++impl Endpoint for Events {+ const PATH: &'static str = "telemetry/events";++ type Request = Value;+ type Response = ();+}diff --git a/crates/legacy/nagare/src/telemetry/mod.rs b/crates/legacy/nagare/src/telemetry/mod.rsindex a73b97e2b..9f2f4d714 100644--- a/crates/legacy/nagare/src/telemetry/mod.rs+++ b/crates/legacy/nagare/src/telemetry/mod.rs@@ -1,7 +1,9 @@-mod context;#[cfg(not(target_family = "wasm"))]-mod endpoint;+mod body;+mod context;mod event;+#[cfg(not(target_family = "wasm"))]+mod events;mod record;mod telemetry;#[cfg(not(target_family = "wasm"))]diff --git a/crates/legacy/nagare/src/telemetry/telemetry.rs b/crates/legacy/nagare/src/telemetry/telemetry.rsindex 0ca37237c..ed85be83c 100644--- a/crates/legacy/nagare/src/telemetry/telemetry.rs+++ b/crates/legacy/nagare/src/telemetry/telemetry.rs@@ -1,11 +1,17 @@+#[cfg(not(target_family = "wasm"))]+use std::time::Duration;++#[cfg(not(target_family = "wasm"))]+use bon::bon;use tokio::sync::mpsc;#[cfg(not(target_family = "wasm"))]use tokio::sync::mpsc::channel as TokioMpscChannel;-use super::{TelemetryContext, TelemetryEvent, record::TelemetryRecord};#[cfg(not(target_family = "wasm"))]-use crate::api::Client;-use crate::api::Config;+use super::TelemetryContext;+use super::{TelemetryEvent, record::TelemetryRecord};+#[cfg(not(target_family = "wasm"))]+use crate::api::{Client, RetryConfig};#[cfg(not(target_family = "wasm"))]const CAPACITY: usize = 256;@@ -22,13 +28,30 @@ impl Telemetry {}}- #[cfg(not(target_family = "wasm"))]+ pub fn report(+ &self,+ event: TelemetryEvent,+ ) {+ if let Some(sender) = &self.sender {+ let _ = sender.try_send(TelemetryRecord::new(event));+ }+ }+}++#[cfg(not(target_family = "wasm"))]+#[bon]+impl Telemetry {+ #[builder]pub fn new(- client_config: Config,- path: String,+ #[builder(into)] base_url: String,context: TelemetryContext,) -> Self {- let client = match Client::new(client_config) {+ let retry = RetryConfig {+ max_attempts: 5,+ base_delay: Duration::from_secs(1),+ budget: Duration::from_secs(60),+ };+ let client = match Client::builder().base_url(base_url).retry(retry).build() {Ok(client) => client,Err(error) => {tracing::warn!(%error, "telemetry disabled: failed to build client");@@ -36,27 +59,9 @@ impl Telemetry {},};let (sender, receiver) = TokioMpscChannel::<TelemetryRecord>(CAPACITY);- tokio::spawn(super::worker::run(client, path, context, receiver));+ tokio::spawn(super::worker::run(client, context, receiver));Self {sender: Some(sender),}}-- #[cfg(target_family = "wasm")]- pub fn new(- _client_config: Config,- _path: String,- _context: TelemetryContext,- ) -> Self {- Self::disabled()- }-- pub fn report(- &self,- event: TelemetryEvent,- ) {- if let Some(sender) = &self.sender {- let _ = sender.try_send(TelemetryRecord::new(event));- }- }}diff --git a/crates/legacy/nagare/src/telemetry/worker.rs b/crates/legacy/nagare/src/telemetry/worker.rsindex 7cdc662eb..1cb689537 100644--- a/crates/legacy/nagare/src/telemetry/worker.rs+++ b/crates/legacy/nagare/src/telemetry/worker.rs@@ -1,98 +1,16 @@use tokio::sync::mpsc;-use super::{TelemetryContext, endpoint::TelemetryEndpoint, record::TelemetryRecord};-use crate::api::{Client, Error};--const MAX_RETRIES: u32 = 4;-const BASE_BACKOFF_MS: u64 = 1_000;-const MAX_BACKOFF_MS: u64 = 16_000;+use super::{TelemetryContext, body::build_body, events::Events, record::TelemetryRecord};+use crate::api::Client;pub(super) async fn run(client: Client,- path: String,context: TelemetryContext,mut receiver: mpsc::Receiver<TelemetryRecord>,) {while let Some(record) = receiver.recv().await {- send_with_retry(&client, &path, &context, &record).await;- }-}--#[derive(Debug, PartialEq)]-enum Disposition {- Retry,- Drop,-}--async fn send_with_retry(- client: &Client,- path: &str,- context: &TelemetryContext,- record: &TelemetryRecord,-) {- let endpoint = TelemetryEndpoint::new(path.to_string(), context, record);- let mut attempt: u32 = 0;- loop {- match client.send(&endpoint).await {- Ok(()) => return,- Err(error) => match classify(&error) {- Disposition::Drop => {- tracing::warn!(%error, "telemetry event dropped");- return;- },- Disposition::Retry => {- if attempt >= MAX_RETRIES {- tracing::warn!(%error, attempts = attempt, "telemetry event dropped after retries");- return;- }- tokio::time::sleep(backoff(attempt)).await;- attempt += 1;- },- },+ if let Err(error) = client.send::<Events>(&build_body(&context, &record)).await {+ tracing::warn!(%error, "telemetry event dropped");}}}--fn classify(error: &Error) -> Disposition {- match error {- Error::Http {- code,- ..- } if *code >= 500 => Disposition::Retry,- Error::Http {- ..- } => Disposition::Drop,- Error::Timeout | Error::Network(_) => Disposition::Retry,- Error::Decode(_) => Disposition::Drop,- }-}--fn backoff(attempt: u32) -> std::time::Duration {- let millis = BASE_BACKOFF_MS.saturating_mul(2u64.saturating_pow(attempt)).min(MAX_BACKOFF_MS);- std::time::Duration::from_millis(millis)-}--#[cfg(test)]-mod tests {- use super::{Disposition, classify};- use crate::api::Error;-- fn http(code: u16) -> Error {- Error::Http {- code,- body: String::new(),- }- }-- #[test]- fn classify_400_drops_503_retries() {- assert_eq!(classify(&http(400)), Disposition::Drop);- assert_eq!(classify(&http(404)), Disposition::Drop);- assert_eq!(classify(&http(503)), Disposition::Retry);- assert_eq!(classify(&http(500)), Disposition::Retry);- assert_eq!(classify(&http(502)), Disposition::Retry);- assert_eq!(classify(&Error::Timeout), Disposition::Retry);- assert_eq!(classify(&Error::Network("x".to_string())), Disposition::Retry);- assert_eq!(classify(&Error::Decode("x".to_string())), Disposition::Drop);- }-}diff --git a/crates/legacy/uzu/Cargo.toml b/crates/legacy/uzu/Cargo.tomlindex 3317e73d2..38440cec3 100644--- a/crates/legacy/uzu/Cargo.toml+++ b/crates/legacy/uzu/Cargo.toml@@ -1,5 +1,7 @@[package]name = "uzu"+description.workspace = true+readme.workspace = trueedition.workspace = truerust-version.workspace = trueversion.workspace = true@@ -78,6 +80,7 @@ tsify = { workspace = true, optional = true }[target.'cfg(not(target_family = "wasm"))'.dependencies]backend-remote.workspace = true+bon.workspace = truenagare.workspace = truehanashi.workspace = truedownload-manager.workspace = truediff --git a/crates/legacy/uzu/src/engine/mod.rs b/crates/legacy/uzu/src/engine/mod.rsindex ea53ea698..0cb58de75 100644--- a/crates/legacy/uzu/src/engine/mod.rs+++ b/crates/legacy/uzu/src/engine/mod.rs@@ -5,7 +5,7 @@ mod download_manager;mod downloader;mod error;-use std::{collections::HashMap, sync::Arc, time::Duration};+use std::{collections::HashMap, sync::Arc};use backend_remote::openai::Backend as OpenAIBackend;pub use callback::{EngineCallback, EngineCallbackType};@@ -13,10 +13,9 @@ pub use config::EngineConfig;pub use download_manager::DownloadManagerType;pub use downloader::{Downloader, DownloaderStream, DownloaderStreamUpdate};pub use error::EngineError;-use indexmap::{IndexMap, IndexSet};+use indexmap::IndexSet;use kiban::rt::RuntimeHandle;use nagare::{- api::Config as ClientConfig,chat::{ChatInstance, ChatSession},classification::ClassificationSession,telemetry::{Telemetry, TelemetryContext, TelemetryDevice, TelemetryEvent},@@ -39,7 +38,7 @@ use crate::{registry::{CachedRegistry, MergedRegistry, RegistryError,local::{Config as LocalRegistryConfig, Registry as LocalRegistry},- mirai::{Backend as MiraiBackend, Config as MiraiRegistryConfig, Registry as MiraiRegistry},+ mirai::{Backend as MiraiBackend, Registry as MiraiRegistry, TELEMETRY_URL},openai::{Config as OpenAIConfig, Registry as OpenAIRegistry},},settings::Settings,@@ -79,11 +78,6 @@ impl Engine {let device = Device::new()?;let telemetry = SharedAccess::new({- let client_config = ClientConfig::new(- "https://sdk.trymirai.com/api/v2".to_string(),- Duration::from_secs(10),- IndexMap::new(),- );let context = TelemetryContext::new(env!("CARGO_PKG_VERSION").to_string(),uzu_engine::TOOLCHAIN_VERSION.to_string(),@@ -94,7 +88,7 @@ impl Engine {is_environment_sandboxed: crate::device::is_environment_sandboxed(),},);- Telemetry::new(client_config, "telemetry/events".to_string(), context)+ Telemetry::builder().base_url(TELEMETRY_URL).context(context).build()});let registry = SharedAccess::new(MergedRegistry::new(vec![]));@@ -120,17 +114,17 @@ impl Engine {let uzu_backend_identifier = uzu_backend.identifier();let uzu_backend_version = uzu_backend.version();- let mirai_registry_config = MiraiRegistryConfig {- api_key: config.mirai_api_key,- device: device.clone(),- backends: vec![MiraiBackend {- identifier: uzu_backend_identifier.clone(),- version: uzu_backend_version.clone(),- }],- include_traces: false,- cache_path: storage_cache_path,- };- let mirai_registry = Box::new(MiraiRegistry::new(mirai_registry_config)?);+ let mirai_registry = Box::new(+ MiraiRegistry::builder()+ .maybe_api_key(config.mirai_api_key)+ .device(device.clone())+ .backends(vec![MiraiBackend {+ identifier: uzu_backend_identifier.clone(),+ version: uzu_backend_version.clone(),+ }])+ .cache_path(storage_cache_path)+ .build()?,+ );engine.add_backend(Arc::new(uzu_backend) as Arc<dyn Backend>).await;engine.add_registry(mirai_registry).await?;diff --git a/crates/legacy/uzu/src/registry/mirai/api.rs b/crates/legacy/uzu/src/registry/mirai/api.rsindex 64a907019..461d10b99 100644--- a/crates/legacy/uzu/src/registry/mirai/api.rs+++ b/crates/legacy/uzu/src/registry/mirai/api.rs@@ -1,59 +1,2 @@-use indexmap::IndexMap;-use nagare::api::{Config, Endpoint as EndpointTrait, Payload};-use reqwest::{Method, header::CONTENT_TYPE};-use serde_json::json;--use crate::{device::Device, registry::mirai::config::Backend};--pub enum Endpoint {- FetchModels {- device: Device,- backends: Vec<Backend>,- include_traces: bool,- show_all: bool,- },-}--impl EndpointTrait for Endpoint {- fn method(&self) -> Method {- match self {- Endpoint::FetchModels {- ..- } => Method::POST,- }- }-- fn path(&self) -> String {- match self {- Endpoint::FetchModels {- ..- } => "fetch/models".to_string(),- }- }-- fn headers(&self) -> IndexMap<String, String> {- IndexMap::from([(CONTENT_TYPE.to_string(), "application/json".to_string())])- }-- fn payload(- &self,- _: &Config,- ) -> Payload {- match self {- Endpoint::FetchModels {- device,- backends,- include_traces,- show_all,- } => Payload {- query: None,- body: Some(json!({- "device": device,- "backends": backends,- "include_traces": include_traces,- "show_all": show_all- })),- },- }- }-}+pub const REGISTRY_URL: &str = "https://sdk.trymirai.com/api/v1";+pub const TELEMETRY_URL: &str = "https://sdk.trymirai.com/api/v2";diff --git a/crates/legacy/uzu/src/registry/mirai/backend.rs b/crates/legacy/uzu/src/registry/mirai/backend.rsnew file mode 100644index 000000000..979348d1f--- /dev/null+++ b/crates/legacy/uzu/src/registry/mirai/backend.rs@@ -0,0 +1,8 @@+use serde::{Deserialize, Serialize};++#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]+#[serde(rename_all = "snake_case")]+pub struct Backend {+ pub identifier: String,+ pub version: String,+}diff --git a/crates/legacy/uzu/src/registry/mirai/config.rs b/crates/legacy/uzu/src/registry/mirai/config.rsdeleted file mode 100644index 0c264a58c..000000000--- a/crates/legacy/uzu/src/registry/mirai/config.rs+++ /dev/null@@ -1,40 +0,0 @@-use std::path::PathBuf;--use serde::{Deserialize, Serialize};--use crate::device::Device;--#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]-#[serde(rename_all = "snake_case")]-pub struct Backend {- pub identifier: String,- pub version: String,-}--#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]-#[serde(rename_all = "snake_case")]-pub struct Config {- pub api_key: Option<String>,- pub device: Device,- pub backends: Vec<Backend>,- pub include_traces: bool,- pub cache_path: PathBuf,-}--impl Config {- pub fn new(- api_key: Option<String>,- device: Device,- backends: Vec<Backend>,- include_traces: bool,- cache_path: PathBuf,- ) -> Self {- Self {- api_key,- device,- backends,- include_traces,- cache_path,- }- }-}diff --git a/crates/legacy/uzu/src/registry/mirai/fetch_models.rs b/crates/legacy/uzu/src/registry/mirai/fetch_models.rsnew file mode 100644index 000000000..f5b0224dc--- /dev/null+++ b/crates/legacy/uzu/src/registry/mirai/fetch_models.rs@@ -0,0 +1,23 @@+use bon::Builder;+use nagare::api::Endpoint;+use serde::Serialize;++use super::{backend::Backend, types::Response};+use crate::device::Device;++#[derive(Serialize, Builder)]+pub struct FetchModels {+ device: Device,+ backends: Vec<Backend>,+ #[builder(default)]+ include_traces: bool,+ #[builder(default)]+ show_all: bool,+}++impl Endpoint for FetchModels {+ const PATH: &'static str = "fetch/models";++ type Request = Self;+ type Response = Response;+}diff --git a/crates/legacy/uzu/src/registry/mirai/mod.rs b/crates/legacy/uzu/src/registry/mirai/mod.rsindex 978fddac2..3d1c1317e 100644--- a/crates/legacy/uzu/src/registry/mirai/mod.rs+++ b/crates/legacy/uzu/src/registry/mirai/mod.rs@@ -1,117 +1,9 @@mod api;-mod config;+mod backend;+mod fetch_models;+mod registry;mod types;-use std::{- fs::{read_to_string, write},- future::Future,- path::PathBuf,- pin::Pin,- time::Duration,-};--pub use api::Endpoint;-pub use config::{Backend, Config};-use indexmap::IndexMap;-use nagare::api::{Client, Config as ClientConfig, Error as ApiError};-use reqwest::header::AUTHORIZATION;-use shoji::{traits::Registry as RegistryTrait, types::model::Model};-pub use types::Response;--use crate::registry::RegistryError;--pub struct Registry {- config: Config,- client: Client,-}--impl Registry {- pub fn new(config: Config) -> Result<Self, RegistryError> {- let mut headers: IndexMap<String, String> = IndexMap::new();- if let Some(api_key) = config.api_key.clone() {- headers.insert(AUTHORIZATION.to_string(), format!("Bearer {}", api_key));- }-- let client_config =- ClientConfig::new("https://sdk.trymirai.com/api/v1".to_string(), Duration::from_secs(10), headers);- let client = Client::new(client_config).map_err(|error| RegistryError::UnableToCreate {- message: error.to_string(),- })?;-- Ok(Self {- config,- client,- })- }-}-impl RegistryTrait for Registry {- type Error = RegistryError;-- fn indentifier(&self) -> String {- "mirai".to_string()- }-- fn models(&self) -> Pin<Box<dyn Future<Output = Result<Vec<Model>, RegistryError>> + Send + '_>> {- Box::pin(async {- match self.fetch_models().await {- Ok(models) => {- if let Err(error) = self.save_registry(&models) {- tracing::warn!(?error, "failed to save Mirai registry");- }- Ok(models)- },- Err(error) => {- let transient = matches!(error, ApiError::Timeout | ApiError::Network(_))- || matches!(error, ApiError::Http { code, .. } if code >= 500);- if transient && let Ok(models) = self.load_registry() {- tracing::warn!(?error, "serving cached Mirai registry after fetch failure");- return Ok(models);- }- Err(RegistryError::UnableToGetModels {- message: error.to_string(),- })- },- }- })- }-}--impl Registry {- async fn fetch_models(&self) -> Result<Vec<Model>, ApiError> {- let response: Response = self- .client- .response(&Endpoint::FetchModels {- device: self.config.device.clone(),- backends: self.config.backends.clone(),- include_traces: self.config.include_traces,- show_all: std::env::var("UZU_REGISTRY_SHOW_ALL").is_ok(),- })- .await?;- response.models().ok_or_else(|| ApiError::Decode("response contained no models".to_string()))- }-- fn registry_path(&self) -> PathBuf {- self.config.cache_path.join("registry.json")- }-- fn save_registry(- &self,- models: &[Model],- ) -> Result<(), RegistryError> {- let contents = serde_json::to_vec_pretty(models).map_err(|error| RegistryError::UnableToGetModels {- message: format!("Unable to serialize registry: {}", error),- })?;- write(self.registry_path(), contents).map_err(|error| RegistryError::UnableToGetModels {- message: format!("Unable to write registry: {}", error),- })- }-- fn load_registry(&self) -> Result<Vec<Model>, RegistryError> {- let contents = read_to_string(self.registry_path()).map_err(|error| RegistryError::UnableToGetModels {- message: format!("Unable to read registry: {}", error),- })?;- serde_json::from_str(&contents).map_err(|error| RegistryError::UnableToGetModels {- message: format!("Unable to parse registry: {}", error),- })- }-}+pub use api::TELEMETRY_URL;+pub use backend::Backend;+pub use registry::Registry;diff --git a/crates/legacy/uzu/src/registry/mirai/registry.rs b/crates/legacy/uzu/src/registry/mirai/registry.rsnew file mode 100644index 000000000..09809db46--- /dev/null+++ b/crates/legacy/uzu/src/registry/mirai/registry.rs@@ -0,0 +1,117 @@+use std::{+ fs::{read_to_string, write},+ future::Future,+ path::PathBuf,+ pin::Pin,+};++use bon::bon;+use nagare::api::{Client, Error as ApiError, IsTransient};+use shoji::{traits::Registry as RegistryTrait, types::model::Model};++use super::{api::REGISTRY_URL, backend::Backend, fetch_models::FetchModels};+use crate::{device::Device, registry::RegistryError};++pub struct Registry {+ device: Device,+ backends: Vec<Backend>,+ include_traces: bool,+ cache_path: PathBuf,+ client: Client,+}++#[bon]+impl Registry {+ #[builder]+ pub fn new(+ api_key: Option<String>,+ device: Device,+ backends: Vec<Backend>,+ #[builder(default)] include_traces: bool,+ cache_path: PathBuf,+ ) -> Result<Self, RegistryError> {+ let client = Client::builder().base_url(REGISTRY_URL).maybe_bearer_token(api_key).build().map_err(|error| {+ RegistryError::UnableToCreate {+ message: error.to_string(),+ }+ })?;++ Ok(Self {+ device,+ backends,+ include_traces,+ cache_path,+ client,+ })+ }+}++impl RegistryTrait for Registry {+ type Error = RegistryError;++ fn indentifier(&self) -> String {+ "mirai".to_string()+ }++ fn models(&self) -> Pin<Box<dyn Future<Output = Result<Vec<Model>, RegistryError>> + Send + '_>> {+ Box::pin(async {+ match self.fetch_models().await {+ Ok(models) => {+ if let Err(error) = self.save_registry(&models) {+ tracing::warn!(?error, "failed to save Mirai registry");+ }+ Ok(models)+ },+ Err(error) => {+ if error.is_transient()+ && let Ok(models) = self.load_registry()+ {+ tracing::warn!(?error, "serving cached Mirai registry after fetch failure");+ return Ok(models);+ }+ Err(RegistryError::UnableToGetModels {+ message: error.to_string(),+ })+ },+ }+ })+ }+}++impl Registry {+ async fn fetch_models(&self) -> Result<Vec<Model>, ApiError> {+ let request = FetchModels::builder()+ .device(self.device.clone())+ .backends(self.backends.clone())+ .include_traces(self.include_traces)+ .show_all(std::env::var("UZU_REGISTRY_SHOW_ALL").is_ok())+ .build();+ let response = self.client.call::<FetchModels>(&request).await?;+ response.models().ok_or_else(|| ApiError::Decode("response contained no models".to_string()))+ }++ fn registry_path(&self) -> PathBuf {+ self.cache_path.join("registry.json")+ }++ fn save_registry(+ &self,+ models: &[Model],+ ) -> Result<(), RegistryError> {+ let contents = serde_json::to_vec_pretty(models).map_err(|error| RegistryError::UnableToGetModels {+ message: format!("Unable to serialize registry: {}", error),+ })?;+ write(self.registry_path(), contents).map_err(|error| RegistryError::UnableToGetModels {+ message: format!("Unable to write registry: {}", error),+ })+ }++ fn load_registry(&self) -> Result<Vec<Model>, RegistryError> {+ let contents = read_to_string(self.registry_path()).map_err(|error| RegistryError::UnableToGetModels {+ message: format!("Unable to read registry: {}", error),+ })?;+ serde_json::from_str(&contents).map_err(|error| RegistryError::UnableToGetModels {+ message: format!("Unable to parse registry: {}", error),+ })+ }+}