The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
refactor(tinyagents): reuse harness middleware and router helpers
Steven Enamakel2762ec6818d0
diff --git a/crates/openhuman-core/src/agent/tinyagents/routes.rs b/crates/openhuman-core/src/agent/tinyagents/routes.rsindex cec34b1af7..8468c1c554 100644--- a/crates/openhuman-core/src/agent/tinyagents/routes.rs+++ b/crates/openhuman-core/src/agent/tinyagents/routes.rs@@ -7,28 +7,17 @@ use tinyagents_harness::context::RunContext;use tinyagents_harness::events::{AgentEvent, GenerationEnrichment, GenerationPricing};use tinyagents_harness::middleware::{MiddlewareModelOutcome, ModelHandler, ModelMiddleware};use tinyagents_harness::retry::FallbackPolicy;-use tinyagents_registry::{WorkloadRoute, WorkloadRouter};-use tinyinference_llm::model::workload::WORKLOAD_ROUTES;+use tinyagents_registry::WorkloadRouter;use tinyinference_llm::model::{CapabilitySet, ModelRequest};+pub(super) use tinyagents_harness::middleware::library::{+ FallbackObserverMiddleware, RequiredCapabilitiesMiddleware,+};/// The workload routing table (fallback chains and the vision `image_in` gate)/// projected into the crate [`WorkloadRouter`] from the upstream declarative/// [`WORKLOAD_ROUTES`] table, which owns the policy.-static OH_WORKLOAD_ROUTER: LazyLock<WorkloadRouter> = LazyLock::new(|| {- WORKLOAD_ROUTES- .iter()- .fold(WorkloadRouter::new(), |router, spec| {- let mut route = WorkloadRoute::new(spec.name, spec.name)- .with_fallbacks(spec.fallbacks.iter().copied());- if spec.requires_image_in {- route = route.requiring(CapabilitySet {- image_in: true,- ..CapabilitySet::default()- });- }- router.with_route(route)- })-});+static OH_WORKLOAD_ROUTER: LazyLock<WorkloadRouter> =+ LazyLock::new(|| WorkloadRouter::from_workload_routes("hint:vision"));/// The capability needs a turn imposes on every model call, derived from what is/// cheaply available at harness-assembly time.@@ -49,45 +38,6 @@ pub(super) fn turn_required_capabilities(model: &str) -> Option<CapabilitySet> {OH_WORKLOAD_ROUTER.required_capabilities(model)}-/// Around-model middleware that stamps the turn's required [`CapabilitySet`] onto-/// every [`ModelRequest`] before resolution/dispatch, so the crate rejects an-/// unfit model pre-dispatch (and, once fallback is wired in 02.2, selects the-/// next capable route) instead of failing at the provider.-///-/// It only sets the requirement when the request carries none, so an inner layer-/// that already declared stricter needs wins.-pub(super) struct RequiredCapabilitiesMiddleware {- required: CapabilitySet,-}--impl RequiredCapabilitiesMiddleware {- pub(super) fn new(required: CapabilitySet) -> Self {- Self { required }- }-}--#[async_trait]-impl ModelMiddleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>- for RequiredCapabilitiesMiddleware-{- fn name(&self) -> &str {- "openhuman.required_capabilities"- }-- async fn wrap_model(- &self,- ctx: &mut RunContext<crate::agent::tinyagents::host::OpenHumanRunContext>,- state: &(),- mut request: ModelRequest,- next: ModelHandler<'_, (), crate::agent::tinyagents::host::OpenHumanRunContext>,- ) -> tinyagents_harness::Result<MiddlewareModelOutcome> {- if request.required_capabilities.is_none() {- request = request.with_required_capabilities(self.required.clone());- }- next.run(ctx, state, request).await- }-}-/// Build the [`FallbackPolicy`] for a turn whose effective/primary model is/// `model` (issue #4249, Workstream 02.2). The returned chain is `[primary,/// alternate…]` — the crate's [`FallbackPolicy::next_after`] traversal expects the@@ -113,66 +63,6 @@ pub(super) fn route_fallback_policy(model: &str) -> Option<FallbackPolicy> {policy}-/// Around-model middleware that makes the crate's registry-backed-/// [`RunPolicy::fallback`][tinyagents_harness::runtime::RunPolicy] traversal-/// **event-visible** (issue #4249, Workstream 02.2).-///-/// The harness performs the cross-route fallback swap inside its model-resolving-/// core (`agent_loop::invoke_model_resolving`) but — unlike the-/// [`ModelFallbackMiddleware`][tinyagents_harness::middleware::ModelFallbackMiddleware]-/// primitive — that native path emits **no**-/// [`AgentEvent::FallbackSelected`]. This observer wraps the resolving core, and-/// on success compares the response's `resolved_model` against the turn's primary-/// model name: when they differ a fallback occurred, so it emits the parity-/// `FallbackSelected` event (mirrored onto OpenHuman's progress/observability-/// bridge) and logs it under `[fallback]`. It never re-issues the call, so it adds-/// no extra provider dispatch on top of the native traversal (no double-fallback).-pub(super) struct FallbackObserverMiddleware {- primary: String,-}--impl FallbackObserverMiddleware {- pub(super) fn new(primary: impl Into<String>) -> Self {- Self {- primary: primary.into(),- }- }-}--#[async_trait]-impl ModelMiddleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>- for FallbackObserverMiddleware-{- fn name(&self) -> &str {- "openhuman.fallback_observer"- }-- async fn wrap_model(- &self,- ctx: &mut RunContext<crate::agent::tinyagents::host::OpenHumanRunContext>,- state: &(),- request: ModelRequest,- next: ModelHandler<'_, (), crate::agent::tinyagents::host::OpenHumanRunContext>,- ) -> tinyagents_harness::Result<MiddlewareModelOutcome> {- let outcome = next.run(ctx, state, request).await?;- let response = outcome.into_response();- if let Some(resolved) = response.resolved_model.as_ref() {- if resolved.name != self.primary {- tracing::info!(- from = %self.primary,- to = %resolved.name,- "[fallback] SDK selected a cross-route fallback model after the primary route failed"- );- ctx.emit(AgentEvent::FallbackSelected {- from: self.primary.clone(),- to: resolved.name.clone(),- });- }- }- Ok(MiddlewareModelOutcome::from(response))- }-}-/// Around-model middleware that feeds the cost event bridge (issue #4249,/// Phase 5): after the real model call, it reads the full host [`BilledUsage`] off/// the returned [`ModelResponse`] — token breakdowns from the crate `Usage`,