feat: MITM request modification — strip bloat from LLM API requests
Intercepts streamGenerateContent requests and trims: - System instruction: strips web_application_development, knowledge_discovery, persistent_context, skills sections (~18KB saved) - Content messages: strips empty user_rules, workflows boilerplate, conversation summaries (~4.5KB saved) - Tools: keeps 12 essential coding tools, strips 8 non-essential (browser_subagent, generate_image, search_web, etc. ~6KB saved) Total: ~55% reduction in request size while keeping identity, user info, and all coding-relevant tools intact. Only modifies 'agent' type requests, checkpoint requests pass through unmodified. Also: - Standalone mode is now the default (use --no-standalone to attach to existing LS) - Enable request modification by default - Add mold linker, sccache, nextest config (8 thread cap) - Add .cargo/config.toml and .config/nextest.toml
This commit is contained in:
@@ -363,7 +363,7 @@ async fn handle_http_over_tls(
|
||||
mut client: tokio_rustls::server::TlsStream<TcpStream>,
|
||||
domain: &str,
|
||||
store: MitmStore,
|
||||
_modify_requests: bool,
|
||||
modify_requests: bool,
|
||||
) -> Result<(), String> {
|
||||
let mut tmp = vec![0u8; 32768];
|
||||
|
||||
@@ -535,12 +535,36 @@ async fn handle_http_over_tls(
|
||||
|
||||
// Log LLM calls at info, everything else at debug
|
||||
if req_path.contains("streamGenerateContent") {
|
||||
let body_len = request_buf.len() - headers_end;
|
||||
info!(
|
||||
domain,
|
||||
req_path = %req_path,
|
||||
body_len,
|
||||
cascade = ?cascade_hint,
|
||||
"MITM: forwarding LLM request"
|
||||
);
|
||||
|
||||
// ── Request modification ─────────────────────────────────────
|
||||
// Dechunk body → check if agent request → modify → rechunk
|
||||
if modify_requests && body_len > 0 {
|
||||
let body_slice = &request_buf[headers_end..];
|
||||
let raw_body = super::modify::dechunk(body_slice);
|
||||
|
||||
// Only modify "agent" requests, not "checkpoint" (LS internal)
|
||||
let is_agent = raw_body
|
||||
.windows(20)
|
||||
.any(|w| w == b"\"requestType\":\"agent" || w == b"requestType\":\"agent\"");
|
||||
|
||||
if is_agent {
|
||||
if let Some(modified_body) = super::modify::modify_request(&raw_body) {
|
||||
// Rebuild request_buf: original headers + rechunked modified body
|
||||
let new_chunked = super::modify::rechunk(&modified_body);
|
||||
let mut new_buf = request_buf[..headers_end].to_vec();
|
||||
new_buf.extend_from_slice(&new_chunked);
|
||||
request_buf = new_buf;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
debug!(
|
||||
domain,
|
||||
|
||||
Reference in New Issue
Block a user