Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.
Rust ai-agentsai-gatewayclaude-codecodexcoding-agentcost-optimizationdeepseekllm-gatewayllm-routerlocal-firstmodel-routingrusttask-routingtauri
use hiroute_application::control::ControlReadError;
use hiroute_domain::{
ActiveAgentConnectionV1, AgentActivationModeV1, AgentIngressProtocolV1,
SupportedAgentInstallationV1, WorkspaceId,
};
use hiroute_integrations::{AgentDiscoveryOutcomeV1, FilesystemAgentDiscoveryV1};
use super::{APPLY_OPERATION, LocalControlAdapter};
impl LocalControlAdapter {
pub(super) fn verify_connection_endpoint(
&self,
connection: &ActiveAgentConnectionV1,
operation: &hiroute_local_storage::SucceededAgentOperationV1,
installation: &SupportedAgentInstallationV1,
) -> Result<(), ControlReadError> {
let runtime = self
.managed_agent_runtime
.lock()
.map_err(|_| ControlReadError::Unavailable)?;
let runtime = runtime.as_ref().ok_or(ControlReadError::Unavailable)?;
if connection.connection.activation_mode != AgentActivationModeV1::ManagedConfiguration {
return Ok(());
}
let path = &installation
.profile
.field("base_endpoint")
.ok_or(ControlReadError::Corrupt)?
.path;
let intent = operation
.plan
.external()
.iter()
.find(|intent| intent.effect_id() == "agent-connection-managed-configuration")
.ok_or(ControlReadError::Corrupt)?;
let change = self
.native_claude_change(intent)
.map_err(super::super::map_port)?
.ok_or(ControlReadError::Corrupt)?;
// A V1 change is a delta: an already matching endpoint may have no changed field.
let endpoint = change
.fields
.iter()
.find(|field| &field.path == path)
.and_then(|field| field.after.as_ref())
.or_else(|| {
installation
.effective_config
.get(path)
.map(|field| &field.value)
})
.and_then(serde_json::Value::as_str)
.ok_or(ControlReadError::Denied)?;
let expected = match connection.connection.protocol {
AgentIngressProtocolV1::Responses => runtime.gateway_base_url.as_str(),
AgentIngressProtocolV1::ChatCompletions => return Err(ControlReadError::Denied),
AgentIngressProtocolV1::Messages => runtime
.gateway_base_url
.strip_suffix("/v1")
.ok_or(ControlReadError::Corrupt)?,
};
if endpoint != expected {
return Err(ControlReadError::Denied);
}
Ok(())
}
pub(super) fn latest_connection_operation(
&self,
connection_id: &str,
) -> Result<
(
ActiveAgentConnectionV1,
hiroute_local_storage::SucceededAgentOperationV1,
),
ControlReadError,
> {
let stores = self
.stores
.lock()
.map_err(|_| ControlReadError::Unavailable)?;
for operation in stores
.control()
.succeeded_agent_operations_for_kind(&WorkspaceId::default(), APPLY_OPERATION)
.map_err(super::super::map_port)?
{
let projection = operation
.plan
.agent_connection_projection()
.map_err(|_| ControlReadError::Corrupt)?;
if let Some(projection) = projection
&& projection.connection_id == connection_id
{
return Ok((projection, operation));
}
}
Err(ControlReadError::NotFound)
}
pub(super) fn exact_discovery(
&self,
agent_id: &str,
profile_id: &str,
_installed_version: &str,
) -> Result<FilesystemAgentDiscoveryV1, ControlReadError> {
let mut matches =
self.scanner
.scan()
.into_iter()
.filter(|discovery| match &discovery.outcome {
AgentDiscoveryOutcomeV1::Supported { installation } => {
installation.agent_id == agent_id
&& installation.profile.profile_id == profile_id
}
AgentDiscoveryOutcomeV1::ReportOnly { .. } => false,
});
let exact = matches.next().ok_or(ControlReadError::NotFound)?;
if matches.next().is_some() {
return Err(ControlReadError::Corrupt);
}
Ok(exact)
}
pub(super) fn latest_connection(
&self,
connection_id: &str,
) -> Result<ActiveAgentConnectionV1, ControlReadError> {
self.latest_connection_operation(connection_id)
.map(|(connection, _)| connection)
}
}
#[cfg(test)]
#[path = "agent_connection_endpoint_tests.rs"]
mod endpoint_tests;
#[cfg(all(test, unix))]
pub(super) use endpoint_tests::{
assert_persisted_endpoint_convergence, open_endpoint_test_artifacts,
};