Skip to main content

lean_ctx/proxy/
anthropic.rs

1use axum::{
2    body::Body,
3    extract::State,
4    http::{Request, StatusCode},
5    response::Response,
6};
7use serde_json::Value;
8
9use super::compress::compress_tool_result;
10use super::forward;
11use super::tool_kind::{self, should_protect, ToolResultKind};
12use super::ProxyState;
13
14/// Conversation turns kept fully intact at the tail of the history; older
15/// tool results are summarized by `history_prune`.
16const KEEP_RECENT: usize = 6;
17
18pub async fn handler(
19    State(state): State<ProxyState>,
20    req: Request<Body>,
21) -> Result<Response, StatusCode> {
22    let upstream = state.anthropic_upstream.clone();
23    forward::forward_request(
24        State(state),
25        req,
26        &upstream,
27        "/v1/messages",
28        compress_request_body,
29        "Anthropic",
30        &[],
31    )
32    .await
33}
34
35fn compress_request_body(parsed: Value, original_size: usize) -> (Vec<u8>, usize, usize) {
36    let mut doc = parsed;
37    let mut modified = false;
38
39    if let Some(messages) = doc.get_mut("messages").and_then(|m| m.as_array_mut()) {
40        // Resolve tool-call id → tool name so file/source reads can be protected
41        // from lossy compression that would force the model to re-read mid-task.
42        let tool_names = tool_kind::anthropic_tool_names(messages);
43
44        super::history_prune::prune_history(messages, KEEP_RECENT, &tool_names);
45        modified = true;
46
47        for msg in messages.iter_mut() {
48            let role = msg.get("role").and_then(|r| r.as_str()).unwrap_or("");
49            if role != "user" {
50                continue;
51            }
52
53            if let Some(content) = msg.get_mut("content").and_then(|c| c.as_array_mut()) {
54                for block in content.iter_mut() {
55                    if block.get("type").and_then(|t| t.as_str()) != Some("tool_result") {
56                        continue;
57                    }
58
59                    let name = block
60                        .get("tool_use_id")
61                        .and_then(|v| v.as_str())
62                        .and_then(|id| tool_names.get(id))
63                        .map(String::as_str);
64                    let kind = name.map_or(ToolResultKind::Other, tool_kind::classify_tool_name);
65
66                    if let Some(inner_content) = block.get_mut("content") {
67                        modified |= compress_content_field(inner_content, name, kind);
68                    }
69                }
70            }
71        }
72    }
73
74    let out = serde_json::to_vec(&doc).unwrap_or_default();
75    let compressed_size = if modified { out.len() } else { original_size };
76    (out, original_size, compressed_size)
77}
78
79/// Compresses a tool_result `content` field unless it is a protected file/source
80/// read, which must reach the model intact (it is what gets edited).
81fn compress_content_field(
82    content: &mut Value,
83    tool_name: Option<&str>,
84    kind: ToolResultKind,
85) -> bool {
86    match content {
87        Value::String(s) => {
88            if should_protect(kind, s) {
89                return false;
90            }
91            let compressed = compress_tool_result(s, tool_name);
92            if compressed.len() < s.len() {
93                *s = compressed;
94                return true;
95            }
96            false
97        }
98        Value::Array(arr) => {
99            let mut modified = false;
100            for item in arr.iter_mut() {
101                if item.get("type").and_then(|t| t.as_str()) == Some("text") {
102                    if let Some(text) = item
103                        .get_mut("text")
104                        .and_then(|t| t.as_str().map(String::from))
105                    {
106                        if should_protect(kind, &text) {
107                            continue;
108                        }
109                        let compressed = compress_tool_result(&text, tool_name);
110                        if compressed.len() < text.len() {
111                            item["text"] = Value::String(compressed);
112                            modified = true;
113                        }
114                    }
115                }
116            }
117            modified
118        }
119        _ => false,
120    }
121}
122
123#[cfg(test)]
124mod tests {
125    use super::*;
126
127    fn source_file_body() -> Vec<u8> {
128        let code = (0..60)
129            .map(|i| format!("    let binding_{i} = compute_value_{i}(context, options);"))
130            .collect::<Vec<_>>()
131            .join("\n");
132        let body = serde_json::json!({
133            "model": "claude-opus-4-8",
134            "messages": [
135                {
136                    "role": "assistant",
137                    "content": [{"type": "tool_use", "id": "toolu_1", "name": "Read", "input": {"file_path": "src/app.rs"}}]
138                },
139                {
140                    "role": "user",
141                    "content": [{"type": "tool_result", "tool_use_id": "toolu_1", "content": code}]
142                }
143            ]
144        });
145        serde_json::to_vec(&body).unwrap()
146    }
147
148    #[test]
149    fn read_tool_result_is_never_truncated() {
150        let bytes = source_file_body();
151        let body: Value = serde_json::from_slice(&bytes).unwrap();
152        let (out, _orig, _comp) = compress_request_body(body, bytes.len());
153        let parsed: Value = serde_json::from_slice(&out).unwrap();
154        let content = parsed["messages"][1]["content"][0]["content"]
155            .as_str()
156            .unwrap();
157        assert!(
158            content.contains("binding_59"),
159            "the full source body must survive — refactors need it intact"
160        );
161        assert!(!content.contains("lines omitted"));
162    }
163
164    #[test]
165    fn bash_tool_result_still_compresses() {
166        let log = {
167            let mut s = String::from(
168                "$ git status\nOn branch main\nYour branch is up to date with 'origin/main'.\n\nChanges not staged for commit:\n  (use \"git add <file>...\" to update what will be committed)\n",
169            );
170            for i in 0..90 {
171                s.push_str(&format!("\tmodified:   src/module_{i}/file_{i}.rs\n"));
172            }
173            s.push_str("\nno changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
174            s
175        };
176        let body = serde_json::json!({
177            "messages": [
178                {"role": "assistant", "content": [{"type": "tool_use", "id": "t1", "name": "Bash", "input": {}}]},
179                {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t1", "content": log}]}
180            ]
181        });
182        let bytes = serde_json::to_vec(&body).unwrap();
183        let (_out, orig, comp) = compress_request_body(body, bytes.len());
184        assert!(comp < orig, "shell output must still be compressed");
185    }
186}