Skip to main content

lean_ctx_proxy/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2#![recursion_limit = "256"]
3#![warn(clippy::undocumented_unsafe_blocks)]
4
5//! The LeanCTX model-API proxy, above the Engine library (`lean_ctx`): an
6//! edit here re-checks this crate and the binary, not the Engine.
7//!
8//! LLM reverse proxy — the core of the **Gateway pillar**.
9//!
10//! Intercepts Anthropic, OpenAI, Gemini and ChatGPT traffic, compresses
11//! prompts, meters usage, and optionally translates request shapes.
12//!
13//! # TLS Requirements
14//!
15//! The proxy does NOT terminate TLS. For production/multi-tenant deployments,
16//! place behind a TLS-terminating reverse proxy (nginx, caddy, traefik).
17//! For mTLS, configure the reverse proxy with client certificates.
18//! See: <https://github.com/yvgude/lean-ctx/issues/1371>
19//!
20//! # Org gateway coupling (ADR-023)
21//!
22//! The self-hosted org gateway (lean-ctx-enterprise) extends this proxy with
23//! admin/usage-store routes. In the OSS build the proxy runs standalone.
24
25#[cfg(feature = "http-server")]
26pub(crate) mod admitted;
27#[cfg(all(test, feature = "http-server"))]
28mod admitted_horizon_tests;
29#[cfg(feature = "http-server")]
30pub(crate) mod agent_compact;
31#[cfg(feature = "http-server")]
32pub mod anthropic;
33#[cfg(all(test, feature = "http-server"))]
34mod auth_tests;
35pub mod auto_routing;
36#[cfg(feature = "http-server")]
37pub mod bedrock;
38pub mod break_even;
39pub mod cache_aligner;
40pub mod cache_attribution;
41#[cfg(feature = "http-server")]
42pub mod cache_breakpoint;
43pub mod cache_policy;
44pub mod cache_safety;
45pub mod cache_share;
46#[cfg(feature = "http-server")]
47pub(crate) mod cache_ttl;
48pub mod ccr;
49#[cfg(all(test, feature = "http-server"))]
50mod ccr_robustness_tests;
51#[cfg(feature = "http-server")]
52pub mod chatgpt;
53#[cfg(feature = "http-server")]
54pub mod chatgpt_cookies;
55#[cfg(feature = "http-server")]
56pub mod chatgpt_ws;
57pub mod circuit_breaker;
58#[cfg(feature = "http-server")]
59mod codec;
60pub mod cold_prefix;
61#[cfg(all(test, feature = "http-server"))]
62mod compact_session_tests;
63#[cfg(all(test, feature = "http-server"))]
64mod compact_session_ttl_tests;
65pub mod compress;
66pub mod compress_api;
67pub mod compress_shared;
68pub mod compression_savings;
69#[cfg(feature = "http-server")]
70mod connector;
71#[cfg(feature = "http-server")]
72pub(crate) mod conversation;
73pub mod cost;
74pub mod counterfactual;
75pub mod dedup;
76#[allow(warnings)]
77pub mod determinism_guard;
78pub mod effort;
79#[cfg(feature = "http-server")]
80mod egress_observation;
81#[cfg(feature = "http-server")]
82mod egress_target;
83pub mod eligibility;
84#[cfg(feature = "http-server")]
85pub mod forward;
86#[cfg(feature = "enterprise")]
87pub mod gateway_identity;
88#[cfg(feature = "http-server")]
89pub mod google;
90#[cfg(all(
91    test,
92    feature = "http-server",
93    any(target_os = "macos", all(target_os = "linux", target_env = "gnu"))
94))]
95mod held_floor_journey_tests;
96pub mod history_prune;
97pub mod holdout;
98#[cfg(all(test, feature = "http-server"))]
99mod host_journey_tests;
100#[cfg(all(test, feature = "http-server"))]
101mod integration_tests;
102#[cfg(feature = "http-server")]
103mod intent;
104pub mod introspect;
105#[cfg(feature = "http-server")]
106pub mod leanctx_provider;
107#[cfg(feature = "http-server")]
108mod lineage;
109#[cfg(feature = "http-server")]
110pub mod models_api;
111#[cfg(feature = "http-server")]
112pub mod ocla_cache_bridge;
113#[cfg(feature = "http-server")]
114pub mod openai;
115#[cfg(feature = "http-server")]
116pub mod openai_responses;
117#[cfg(feature = "http-server")]
118pub mod openai_responses_ws;
119#[cfg(feature = "http-server")]
120pub mod outbound_model_policy;
121pub mod output_savings;
122pub mod pii;
123pub mod pipeline_bench;
124#[cfg(feature = "enterprise")]
125pub mod policy_gate;
126pub mod pre_optimize;
127pub mod prefix_cache_stats;
128pub mod prefix_replay;
129#[cfg(feature = "http-server")]
130pub(crate) mod project_attribution;
131pub mod prose;
132#[allow(warnings)]
133pub mod prose_compress;
134pub mod prose_patterns;
135pub mod prose_ranker;
136#[cfg(feature = "http-server")]
137pub mod providers;
138#[cfg(feature = "http-server")]
139#[cfg(all(test, feature = "http-server"))]
140mod reference_tests;
141pub(crate) mod request_project;
142#[cfg(feature = "http-server")]
143pub(crate) mod response_shaper;
144pub mod rollout;
145pub mod routing;
146#[cfg(feature = "http-server")]
147pub(crate) mod sealed_origin;
148pub mod session_elide;
149#[cfg(test)]
150mod shape_route_tests;
151#[cfg(feature = "shape-xlat")]
152pub mod shape_xlat;
153#[cfg(feature = "http-server")]
154pub(crate) mod shaping_hook;
155#[cfg(feature = "http-server")]
156pub mod sse_keepalive;
157#[cfg(all(test, feature = "http-server"))]
158mod stats_tests;
159pub mod sticky_tools;
160pub(crate) mod store_binding;
161#[cfg(feature = "http-server")]
162pub(crate) mod tool_descriptions;
163pub mod tool_kind;
164#[cfg(feature = "http-server")]
165pub mod tool_output;
166#[cfg(all(test, feature = "http-server"))]
167mod upstream_tests;
168pub mod usage;
169#[cfg(feature = "http-server")]
170pub mod usage_accounting;
171pub mod usage_meter;
172pub mod usage_parity;
173pub mod usage_sink;
174pub mod verbosity;
175#[cfg(all(test, feature = "http-server"))]
176mod via_p0_characterization_tests;
177#[cfg(feature = "http-server")]
178pub mod web_app;
179
180#[allow(warnings)]
181pub mod live_zone;
182
183// The HTTP proxy server: state, router, middleware and handlers. Everything
184// above builds without `http-server` (#586); this half needs axum.
185#[cfg(feature = "http-server")]
186mod server;
187#[cfg(feature = "http-server")]
188pub use server::*;
189
190/// Hand the proxy's output shaping to the engine's `ctx_shape` tool. The
191/// CLI calls this at start-up; the first registration wins.
192pub fn register_shapers() {
193    lean_ctx::core::output_shapers::register_output_shapers(
194        lean_ctx::core::output_shapers::OutputShapers {
195            command: compress::shape_command_output,
196            tool: compress::shape_tool_output,
197        },
198    );
199}