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
14pub async fn handler(
15 State(state): State<ProxyState>,
16 req: Request<Body>,
17) -> Result<Response, StatusCode> {
18 let upstream = state.openai_upstream.clone();
19 forward::forward_request(
20 State(state),
21 req,
22 &upstream,
23 "/v1/chat/completions",
24 compress_request_body,
25 "OpenAI",
26 &[],
27 )
28 .await
29}
30
31fn compress_request_body(parsed: Value, original_size: usize) -> (Vec<u8>, usize, usize) {
32 let mut doc = parsed;
33 let mut modified = false;
34
35 if let Some(messages) = doc.get_mut("messages").and_then(|m| m.as_array_mut()) {
36 let tool_names = tool_kind::openai_tool_names(messages);
37
38 let mode = crate::core::config::Config::load()
41 .proxy
42 .resolved_history_mode();
43 let boundary = super::history_prune::prune_boundary(mode, messages.len());
44 modified |= super::history_prune::prune_history(messages, boundary, &tool_names);
45
46 for msg in messages.iter_mut() {
47 let role = msg.get("role").and_then(|r| r.as_str()).unwrap_or("");
48 if role != "tool" {
49 continue;
50 }
51
52 let name = msg
53 .get("tool_call_id")
54 .and_then(|v| v.as_str())
55 .and_then(|id| tool_names.get(id))
56 .map(String::as_str);
57 let kind = name.map_or(ToolResultKind::Other, tool_kind::classify_tool_name);
58
59 if let Some(content) = msg
60 .get_mut("content")
61 .and_then(|c| c.as_str().map(String::from))
62 {
63 if should_protect(kind, &content) {
64 continue;
65 }
66 let compressed = compress_tool_result(&content, name);
67 if compressed.len() < content.len() {
68 msg["content"] = Value::String(compressed);
69 modified = true;
70 }
71 }
72 }
73 }
74
75 let out = serde_json::to_vec(&doc).unwrap_or_default();
76 let compressed_size = if modified { out.len() } else { original_size };
77 (out, original_size, compressed_size)
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn read_file_tool_result_protected() {
86 let code = (0..60)
87 .map(|i| format!(" const value{i} = computeValue{i}(ctx, opts);"))
88 .collect::<Vec<_>>()
89 .join("\n");
90 let body = serde_json::json!({
91 "model": "gpt-5",
92 "messages": [
93 {"role": "assistant", "tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "read_file"}}]},
94 {"role": "tool", "tool_call_id": "call_1", "content": code}
95 ]
96 });
97 let bytes = serde_json::to_vec(&body).unwrap();
98 let (out, _orig, _comp) = compress_request_body(body, bytes.len());
99 let parsed: Value = serde_json::from_slice(&out).unwrap();
100 assert!(parsed["messages"][1]["content"]
101 .as_str()
102 .unwrap()
103 .contains("value59"));
104 }
105}