Files
leleka/desktop-core/src/gateway.rs
T
Pavlo Ratushnyi 7610ba3568 STRA-338 size-gw: the 19 oversize files of control-plane and desktop-core split by topic (no file over 400 lines); mechanical child modules renamed
Tests become directory modules named after what they test, with a shared fixtures/harness module:
control-plane/src/{responses,ggml_bridge,gguf_f32_stream,prepare_preview}/tests/, desktop-core/src/runtime_rpc/tests/;
integration targets become tests/<name>/main.rs plus topic modules and a harness (backend_worker_http, vision_http,
memory_gate_http, tool_corpus, mcp_hub, api_over_runtime, runtime_gateway_sdet, native_tools_sdet,
native_tools_retry_sdet, managed_worker, gateway); target names are unchanged. Sources: leleka-inference.rs becomes
src/bin/leleka-inference/ (main.rs plus one module per operator option group), the cp_replay example becomes
examples/cp_replay/, and the tonic service impl of runtime_rpc delegates to session_rpc, model_rpc and generate_rpc
(bodies unchanged). impl_* modules are renamed after what they hold (base_provenance, count_tokens, manifest_fetch,
fetcher, record, capture, in_flight, tool_policy, tool_plan, server). The self-spawning tests that name themselves
(credential_child, the managed worker fixture) use the new module path or stay at the target root.

Test ids: 543 before and after in both crates (strata-control, strata-desktop-core, --all-targets), 0 missing, 0 new,
0 outcome changes; 241 ids differ only in the module path. Warnings in the two crates: 0 before and after.
Counts (strata-control / strata-desktop-core): oversize files 19 -> 0 (13 + 6). Functions over 30 lines, complexity 10,
nesting 4, wide lines, prod: 187/135/17/199 -> 187/135/17/196 and 81/59/6/54 -> 81/59/6/54; test: 214/17/30/133 ->
214/17/30/133 and 65/3/3/30 -> 65/3/3/27. The 211 functions that moved verbatim are in limits-allowlist.json (owner
STRA-338) because the diff gate treats a moved function as added; the entries fall as the functions are shortened.
2026-11-06 13:13:59 +01:00

90 lines
2.2 KiB
Rust

//! Experimental loopback gateway. Endpoints must already be managed and ready.
use crate::pool::{AdmissionError, Lease, Pool, ResourceGroup, Worker};
use axum::{
Json, Router,
body::{Body, Bytes},
extract::{DefaultBodyLimit, State},
http::{HeaderMap, StatusCode},
response::{IntoResponse, Response},
routing::{get, post},
};
use futures_util::StreamExt;
use serde::Deserialize;
use serde_json::{Value, json};
use std::{
collections::{BTreeMap, HashSet, VecDeque},
net::{IpAddr, SocketAddr},
sync::{Arc, Mutex},
time::Duration,
};
use subtle::ConstantTimeEq;
#[derive(Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
pub schema_version: u32,
pub listen: SocketAddr,
pub token_env: String,
pub queue_limit: usize,
pub queue_timeout_ms: u64,
pub request_timeout_ms: u64,
pub groups: Vec<Group>,
pub deployments: Vec<Deployment>,
/// Explicitly permitted members; distinct models never share an implicit alias.
pub aliases: BTreeMap<String, Vec<String>>,
}
#[derive(Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Group {
pub id: String,
pub slots: usize,
pub available_bytes: u64,
}
#[derive(Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Deployment {
pub id: String,
pub model: String,
pub endpoint: String,
pub token_env: Option<String>,
pub group: String,
pub slots: usize,
pub request_bytes: u64,
pub capabilities: Vec<String>,
}
#[derive(Clone)]
pub struct Gateway(Arc<Inner>);
struct Inner {
config: Config,
pool: Pool,
token: String,
upstream_tokens: Vec<Option<String>>,
client: reqwest::Client,
// Bounded process-local ownership, never response text or prompts.
owners: Mutex<VecDeque<(String, usize)>>,
}
struct InFlight {
g: Gateway,
worker: usize,
lease: Option<Lease>,
complete: bool,
}
#[derive(Default)]
struct Terminal {
line: Vec<u8>,
data: Vec<u8>,
overflow: bool,
complete: bool,
response_id: Option<String>,
}
mod secret;
#[allow(unused_imports)]
pub use secret::*;
mod in_flight;
#[allow(unused_imports)]
pub use in_flight::*;
mod responses;
#[allow(unused_imports)]
pub use responses::*;