Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(inference): remove turn model factory functions

Steven Enamakelbb45d79e00b2
diff --git a/crates/openhuman-core/src/inference/provider/factory.rs b/crates/openhuman-core/src/inference/provider/factory.rs
index 41cab6b07d..638a70705e 100644
--- a/crates/openhuman-core/src/inference/provider/factory.rs
+++ b/crates/openhuman-core/src/inference/provider/factory.rs
@@ -11,7 +11,6 @@
//! - [`cloud_spec`] — `CloudProviderCreds` → `ProviderSpec` → `ProviderRegistry`.
//! - [`subprocess_providers`] — Claude Agent SDK and Claude Code CLI.
//! - [`chat_model`] — the one-shot `create_chat_model*` entry points.
-//! - [`turn_model`] — the per-turn `create_turn_chat_model*` entry points.
pub(crate) mod access_gates;
mod chat_model;
@@ -23,7 +22,6 @@ mod local_runtime;
mod managed_backend;
mod routing;
mod subprocess_providers;
-mod turn_model;
pub(crate) use chat_model::{resolves_to_managed_backend, OpenHumanModelFactory};
pub use chat_model::{
@@ -39,11 +37,6 @@ pub(crate) use local_runtime::create_local_chat_model_from_string;
pub(crate) use managed_backend::make_openhuman_backend_model;
pub(crate) use routing::role_uses_implicit_cloud_fallback;
pub use routing::{provider_for_role, role_bypasses_managed_credits};
-pub(crate) use turn_model::{
- create_turn_chat_model, create_turn_chat_model_from_string,
- create_turn_chat_model_from_string_with_native_tools_and_route,
- create_turn_chat_model_with_native_tools_and_route,
-};
// Factory-internal helpers shared across the implementation modules.
use access_gates::{emit_inference_egress, enforce_local_only_inference};
diff --git a/crates/openhuman-core/src/inference/provider/factory/turn_model.rs b/crates/openhuman-core/src/inference/provider/factory/turn_model.rs
deleted file mode 100644
index b400fafd47..0000000000
--- a/crates/openhuman-core/src/inference/provider/factory/turn_model.rs
+++ /dev/null
@@ -1,318 +0,0 @@
-//! Per-turn `create_turn_chat_model*` entry points pinned to an explicit model
-//! (the `TurnModelSource` constructors), by role or by explicit provider string.
-
-use super::*;
-use crate::core::runtime::DispatchCtx;
-
-/// Build a crate-native [`ChatModel`] for the **turn path**, pinned to an explicit
-/// `model` string — the turn's effective/dispatched model after any config-level
-/// agent pin (issue #4249, Phase 3 P3-B). The per-`(role, model)` analogue of
-/// [`create_chat_model_with_model_id`] used by the crate-native
-/// [`TurnModelSource`](crate::agent::tinyagents::TurnModelSource) to construct
-/// the primary + each workload-tier route directly.
-///
-/// - **Managed** → the registered managed chat model
-/// pinned to `model`; the backend resolves the tier from `request.model`, so a
-/// tier alias / agent-model pin dispatches directly.
-/// - **Local / cloud** → the crate builders; the model rides the role's resolved
-/// provider string. A config-level *primary-model pin* on a local/cloud provider
-/// is not re-pinned here (pins are tier selection on the managed backend); the
-/// role's resolved model has the same behaviour.
-/// - **Claude Agent SDK** → its direct prompt-guided [`ChatModel`] subprocess
-/// adapter, pinned to `model`.
-/// - **Claude Code** → its direct native-tool streaming [`ChatModel`] subprocess
-/// adapter, pinned to `model`.
-///
-/// Respects the native test-model override, exactly as
-/// [`create_chat_model_with_model_id`].
-pub(crate) fn create_turn_chat_model(
- ctx: &DispatchCtx,
- role: &str,
- config: &Config,
- model: &str,
- temperature: f64,
-) -> anyhow::Result<Arc<dyn ChatModel<()>>> {
- create_turn_chat_model_with_native_tools(ctx, role, config, model, temperature, true)
-}
-
-pub(crate) fn create_turn_chat_model_with_native_tools(
- ctx: &DispatchCtx,
- role: &str,
- config: &Config,
- model: &str,
- temperature: f64,
- native_tool_calling: bool,
-) -> anyhow::Result<Arc<dyn ChatModel<()>>> {
- create_turn_chat_model_with_native_tools_and_route(
- ctx,
- role,
- config,
- model,
- temperature,
- native_tool_calling,
- &EgressLedger::none(),
- )
- .map(|(chat, _, _)| chat)
-}
-
-/// Build a turn model together with the concrete provider and post-remap model
-/// id that the constructed client will put on the wire. The route metadata is
-/// consumed by channel audit recording; returning it from the construction
-/// branches avoids re-parsing a provider string before cloud default-model and
-/// abstract-tier remapping has run.
-pub(crate) fn create_turn_chat_model_with_native_tools_and_route(
- ctx: &DispatchCtx,
- role: &str,
- config: &Config,
- model: &str,
- temperature: f64,
- native_tool_calling: bool,
- egress: &EgressLedger,
-) -> anyhow::Result<(Arc<dyn ChatModel<()>>, String, String)> {
- create_turn_chat_model_with_native_tools_and_route_inner(
- ctx,
- role,
- config,
- model,
- native_tool_calling,
- egress,
- )
- .map(|(chat, provider, model)| (with_default_temperature(chat, temperature), provider, model))
-}
-
-pub(super) fn create_turn_chat_model_with_native_tools_and_route_inner(
- ctx: &DispatchCtx,
- role: &str,
- config: &Config,
- model: &str,
- native_tool_calling: bool,
- egress: &EgressLedger,
-) -> anyhow::Result<(Arc<dyn ChatModel<()>>, String, String)> {
- if let Some(chat) = ctx.host_overrides().and_then(|local| local.model_for(role)) {
- return Ok((chat, "host".into(), model.to_owned()));
- }
- #[cfg(any(test, feature = "e2e-test-support", feature = "rss-bench"))]
- if let Some(chat) = test_provider_override::current() {
- let provider = chat
- .profile()
- .and_then(|profile| profile.provider.clone())
- .unwrap_or_else(|| "injected".to_string());
- return Ok((chat, provider, model.to_string()));
- }
- let test_override_active = {
- #[cfg(any(test, feature = "e2e-test-support", feature = "rss-bench"))]
- {
- test_provider_override::current().is_some()
- }
- #[cfg(not(any(test, feature = "e2e-test-support", feature = "rss-bench")))]
- {
- false
- }
- };
- if !test_override_active {
- if resolves_to_managed_backend(role, config) {
- let resolved_context = ctx.resolved();
- let extensions = resolved_context
- .as_deref()
- .map(crate::core::runtime::CoreContext::extensions);
- let (chat, _resolved_model) = super::managed_backend::managed_chat_model_for(
- extensions,
- role,
- config,
- None,
- Some(model),
- native_tool_calling,
- None,
- egress,
- )?;
- return Ok((chat, PROVIDER_OPENHUMAN.to_string(), model.to_string()));
- }
- let resolved_provider = provider_for_role(role, config);
- let provider_name = resolved_provider
- .trim()
- .split(':')
- .next()
- .unwrap_or(resolved_provider.trim())
- .to_string();
- if let Some(result) = prepare_claude_agent_sdk_chat_model(role, &resolved_provider, config)
- {
- let _resolved_model = result?;
- emit_inference_egress(role, &format!("{CLAUDE_AGENT_SDK_PREFIX}{model}"), egress);
- return Ok((
- Arc::new(ClaudeAgentSdkProvider::for_model(
- (&config.claude_agent_sdk).into(),
- model,
- )),
- provider_name,
- model.to_string(),
- ));
- }
- if let Some(result) = try_create_claude_code_chat_model_from_string(
- role,
- &resolved_provider,
- config,
- Some(model),
- egress,
- ) {
- return result
- .map(|(chat, _configured_model)| (chat, provider_name.clone(), model.to_string()));
- }
- if let Some(result) = try_create_local_runtime_chat_model(role, config, egress) {
- return result
- .map(|(chat, resolved_model)| (chat, provider_name.clone(), resolved_model));
- }
- let resolved_context = ctx.resolved();
- let extensions = resolved_context
- .as_deref()
- .map(crate::core::runtime::CoreContext::extensions);
- if let Some(result) = try_create_cloud_slug_chat_model_with_native_tools_for(
- role,
- config,
- native_tool_calling,
- egress,
- extensions,
- ) {
- return result
- .map(|(chat, resolved_model)| (chat, provider_name.clone(), resolved_model));
- }
- }
- let resolved_context = ctx.resolved();
- let extensions = resolved_context
- .as_deref()
- .map(crate::core::runtime::CoreContext::extensions);
- Err(unresolved_chat_model_error_for(
- role,
- &provider_for_role(role, config),
- config,
- egress,
- extensions,
- ))
-}
-
-/// Like [`create_turn_chat_model`] but for an **explicit** `provider_string` — the
-/// explicit-string counterpart of [`create_turn_chat_model`], for producers
-/// whose effective provider differs from the role's default resolution.
-///
-/// The triage path needs this: [`build_remote_provider`](crate::triage::routing)
-/// forces the managed backend (`provider_string == `[`PROVIDER_OPENHUMAN`]) when the
-/// chat route is local / BYOK-incomplete — the #1257 *"triage never goes
-/// local"* invariant — which a plain [`create_turn_chat_model`] (role → `provider_for_role`)
-/// would violate by building the local model.
-///
-/// - `provider_string` empty / `"cloud"` / [`PROVIDER_OPENHUMAN`] → managed
-/// the registered managed chat model pinned to `model` (the force-managed case).
-/// - Otherwise the string equals what the role resolves to (a BYOK cloud slug), so
-/// this delegates to [`create_turn_chat_model`] for `role`.
-///
-/// Respects the test-provider override (bespoke/`Provider` path), like its siblings.
-pub(crate) fn create_turn_chat_model_from_string(
- ctx: &DispatchCtx,
- role: &str,
- provider_string: &str,
- config: &Config,
- model: &str,
- temperature: f64,
-) -> anyhow::Result<Arc<dyn ChatModel<()>>> {
- create_turn_chat_model_from_string_with_native_tools(
- ctx,
- role,
- provider_string,
- config,
- model,
- temperature,
- true,
- )
-}
-
-pub(crate) fn create_turn_chat_model_from_string_with_native_tools(
- ctx: &DispatchCtx,
- role: &str,
- provider_string: &str,
- config: &Config,
- model: &str,
- temperature: f64,
- native_tool_calling: bool,
-) -> anyhow::Result<Arc<dyn ChatModel<()>>> {
- create_turn_chat_model_from_string_with_native_tools_and_route(
- ctx,
- role,
- provider_string,
- config,
- model,
- temperature,
- native_tool_calling,
- &EgressLedger::none(),
- )
- .map(|(chat, _, _)| chat)
-}
-
-pub(crate) fn create_turn_chat_model_from_string_with_native_tools_and_route(
- ctx: &DispatchCtx,
- role: &str,
- provider_string: &str,
- config: &Config,
- model: &str,
- temperature: f64,
- native_tool_calling: bool,
- egress: &EgressLedger,
-) -> anyhow::Result<(Arc<dyn ChatModel<()>>, String, String)> {
- if let Some(chat) = ctx.host_overrides().and_then(|local| local.model_for(role)) {
- return Ok((chat, "host".into(), model.to_owned()));
- }
- #[cfg(any(test, feature = "e2e-test-support", feature = "rss-bench"))]
- if let Some(chat) = test_provider_override::current() {
- let provider = chat
- .profile()
- .and_then(|profile| profile.provider.clone())
- .unwrap_or_else(|| "injected".to_string());
- return Ok((
- with_default_temperature(chat, temperature),
- provider,
- model.to_string(),
- ));
- }
- let test_override_active = {
- #[cfg(any(test, feature = "e2e-test-support", feature = "rss-bench"))]
- {
- test_provider_override::current().is_some()
- }
- #[cfg(not(any(test, feature = "e2e-test-support", feature = "rss-bench")))]
- {
- false
- }
- };
- let p = provider_string.trim();
- let is_managed = p.is_empty() || p == "cloud" || p == PROVIDER_OPENHUMAN;
- if is_managed && !test_override_active {
- let resolved_context = ctx.resolved();
- let extensions = resolved_context
- .as_deref()
- .map(crate::core::runtime::CoreContext::extensions);
- let (chat, _resolved_model) = super::managed_backend::managed_chat_model_for(
- extensions,
- role,
- config,
- None,
- Some(model),
- native_tool_calling,
- None,
- egress,
- )?;
- return Ok((
- with_default_temperature(chat, temperature),
- PROVIDER_OPENHUMAN.to_string(),
- model.to_string(),
- ));
- }
- // A concrete non-managed string equals the role's resolution (triage only
- // honours a BYOK **cloud** route as-is), so the role-based builder matches.
- create_turn_chat_model_with_native_tools_and_route(
- ctx,
- role,
- config,
- model,
- temperature,
- native_tool_calling,
- egress,
- )
-}