env-read 14 -> 0 outside src/env_settings.rs (config/process_env.rs removed; typed text/number/path/mode accessors), glob-super-import 9 -> 0, allow-unused-imports 48 -> 0, 38 modules renamed by responsibility (worker_entry, serve_generation, file_download, pack_outputs, request_context, status_routes, ...). The hot-path code (admission, streaming, gRPC client) is untouched here. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
141 lines
5.6 KiB
Rust
141 lines
5.6 KiB
Rust
//! The sections of `Config::load_with_path` that stand alone: limits, the engine worker pool, the MCP servers.
|
|
//! Each reads the parsed file and the environment, checks its own ranges, and fails with the same messages and in the
|
|
//! same order as when it was one function.
|
|
|
|
use super::load_engine as engine;
|
|
use super::{Config, FileConfig, OsString, Path, SocketAddr, WorkerConfig};
|
|
|
|
pub(super) struct Limits {
|
|
pub(super) bind: SocketAddr,
|
|
pub(super) max_in_flight_requests: usize,
|
|
pub(super) prompt_date: crate::responses::PromptDate,
|
|
pub(super) queue_timeout_s: Option<u64>,
|
|
pub(super) shutdown_grace_s: u64,
|
|
}
|
|
|
|
pub(super) struct EngineSection {
|
|
pub(super) worker: Option<WorkerConfig>,
|
|
pub(super) extra_workers: Vec<WorkerConfig>,
|
|
pub(super) idle_unload_s: Option<f64>,
|
|
}
|
|
|
|
type Servers = std::collections::BTreeMap<String, serde_json::Map<String, serde_json::Value>>;
|
|
|
|
pub(super) type Mcp = (Servers, crate::mcp::Settings);
|
|
|
|
fn bind_address(file: &FileConfig) -> Result<SocketAddr, crate::BoxError> {
|
|
Ok(crate::env_settings::text("STRATA_BIND")?
|
|
.or(file.bind.clone())
|
|
.unwrap_or_else(|| "127.0.0.1:8081".to_owned())
|
|
.parse()?)
|
|
}
|
|
|
|
fn in_flight_limit(file: &FileConfig) -> Result<usize, crate::BoxError> {
|
|
let max_in_flight_requests = crate::env_settings::number::<usize>("STRATA_MAX_IN_FLIGHT_REQUESTS")?
|
|
.or(file.max_in_flight_requests)
|
|
.unwrap_or(crate::lifecycle::DEFAULT_MAX_IN_FLIGHT_REQUESTS);
|
|
crate::lifecycle::validate_capacity(max_in_flight_requests)?;
|
|
Ok(max_in_flight_requests)
|
|
}
|
|
|
|
fn prompt_date_setting(file: &FileConfig) -> Result<crate::responses::PromptDate, crate::BoxError> {
|
|
Ok(crate::env_settings::text("STRATA_PROMPT_DATE")?
|
|
.or(file.prompt_date.clone())
|
|
.map(|text| crate::responses::PromptDate::parse(&text))
|
|
.transpose()?
|
|
.unwrap_or(crate::responses::PromptDate::Utc))
|
|
}
|
|
|
|
fn queue_timeout(file: &FileConfig) -> Result<Option<u64>, crate::BoxError> {
|
|
let queue_timeout_s = crate::env_settings::number::<u64>("STRATA_QUEUE_TIMEOUT_S")?
|
|
.or(file.queue_timeout_s);
|
|
if queue_timeout_s.is_some_and(|seconds| !(1..=86400).contains(&seconds)) {
|
|
return Err("queue_timeout_s must be between 1 and 86400 when configured".into());
|
|
}
|
|
Ok(queue_timeout_s)
|
|
}
|
|
|
|
fn shutdown_grace(file: &FileConfig) -> Result<u64, crate::BoxError> {
|
|
let shutdown_grace_s = crate::env_settings::number::<u64>("STRATA_SHUTDOWN_GRACE_S")?
|
|
.or(file.shutdown_grace_s)
|
|
.unwrap_or(0);
|
|
if shutdown_grace_s > 20 {
|
|
return Err("shutdown_grace_s must be between 0 and 20".into());
|
|
}
|
|
Ok(shutdown_grace_s)
|
|
}
|
|
|
|
/// The servers of Claude Desktop's file: `mcpServers` (or `mcp_servers`) at the top.
|
|
fn servers_of_file(path: &Path) -> Result<Servers, crate::BoxError> {
|
|
let location = format!("mcp_config_file {}", path.display());
|
|
let text =
|
|
std::fs::read_to_string(path).map_err(|error| format!("[strata] {location}: {error}"))?;
|
|
let data: serde_json::Value = serde_json::from_str(text.trim_start_matches('\u{feff}'))
|
|
.map_err(|error| format!("[strata] {location}: {error}"))?;
|
|
let block = data
|
|
.get("mcpServers")
|
|
.or_else(|| data.get("mcp_servers"))
|
|
.ok_or_else(|| format!("[strata] {location}: expected {{\"mcpServers\": {{...}}}}"))?;
|
|
Ok(crate::mcp::servers_from(block, &location)?)
|
|
}
|
|
|
|
impl Config {
|
|
pub(super) fn read_limits(file: &FileConfig) -> Result<Limits, crate::BoxError> {
|
|
let bind = bind_address(file)?;
|
|
let max_in_flight_requests = in_flight_limit(file)?;
|
|
let prompt_date = prompt_date_setting(file)?;
|
|
let queue_timeout_s = queue_timeout(file)?;
|
|
let shutdown_grace_s = shutdown_grace(file)?;
|
|
Ok(Limits {
|
|
bind,
|
|
max_in_flight_requests,
|
|
prompt_date,
|
|
queue_timeout_s,
|
|
shutdown_grace_s,
|
|
})
|
|
}
|
|
|
|
pub(super) fn read_engine(file: &FileConfig) -> Result<EngineSection, crate::BoxError> {
|
|
let executable = crate::env_settings::os_string("STRATA_ENGINE_EXE").or_else(|| {
|
|
file.engine
|
|
.as_ref()
|
|
.map(|engine| OsString::from(&engine.executable))
|
|
});
|
|
let knobs = engine::read_knobs(file)?;
|
|
let idle_unload_s = knobs.idle_unload_s;
|
|
let worker = engine::build_worker(executable, file.engine.as_ref(), knobs)?;
|
|
let extra_workers = engine::extra_workers(file, worker.as_ref())?;
|
|
Ok(EngineSection {
|
|
worker,
|
|
extra_workers,
|
|
idle_unload_s,
|
|
})
|
|
}
|
|
|
|
/// The four MCP entries of the file: `mcp_servers`, `mcpServers`, `mcp_config_file` and the `mcp` settings block.
|
|
pub(super) fn read_mcp(
|
|
servers: Option<&serde_json::Value>,
|
|
desktop: Option<&serde_json::Value>,
|
|
config_file: Option<&Path>,
|
|
settings: Option<&serde_json::Value>,
|
|
) -> Result<Mcp, crate::BoxError> {
|
|
let mut mcp_servers = std::collections::BTreeMap::new();
|
|
let null = serde_json::Value::Null;
|
|
mcp_servers.extend(crate::mcp::servers_from(
|
|
servers.unwrap_or(&null),
|
|
"config mcp_servers",
|
|
)?);
|
|
mcp_servers.extend(crate::mcp::servers_from(
|
|
desktop.unwrap_or(&null),
|
|
"config mcpServers",
|
|
)?);
|
|
if let Some(path) = config_file {
|
|
// Claude Desktop's format; a name in both takes this file's entry.
|
|
mcp_servers.extend(servers_of_file(path)?);
|
|
}
|
|
let mcp_settings = crate::mcp::settings_from(settings.unwrap_or(&null))?;
|
|
|
|
Ok((mcp_servers, mcp_settings))
|
|
}
|
|
}
|