Skip to content

Commit 24f1e0e

Browse files
feat: add per-request compression override fields (#416)
1 parent bd3e756 commit 24f1e0e

5 files changed

Lines changed: 59 additions & 37 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "edgee"
3-
version = "2.0.4"
3+
version = "2.0.5"
44
edition = "2021"
55
authors = ["Edgee <opensource@edgee.ai>"]
66
license = "Apache-2.0"
@@ -48,4 +48,4 @@ path = "examples/tools.rs"
4848

4949
[[example]]
5050
name = "compression"
51-
path = "examples/compression.rs"
51+
path = "examples/compression.rs"

examples/compression.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//! This example includes a large context in the user message to demonstrate meaningful
1010
//! compression savings.
1111
12-
use edgee::{Edgee, InputObject, Message};
12+
use edgee::{CompressionConfiguration, Edgee, InputObject, Message};
1313

1414
// Large context document to demonstrate input compression
1515
const LARGE_CONTEXT: &str = r#"
@@ -82,8 +82,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
8282

8383
// Create input with compression settings using builder pattern
8484
let input = InputObject::new(vec![Message::user(user_message)])
85-
.with_compression(true)
86-
.with_compression_rate(0.5);
85+
.with_compression_model("agentic")
86+
.with_compression_configuration(CompressionConfiguration {
87+
rate: Some(0.5),
88+
semantic_preservation_threshold: None,
89+
});
8790

8891
let response = client.send("anthropic/claude-haiku-4-5", input).await?;
8992

src/client.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ struct ParsedInput {
4747
tools: Option<Vec<Tool>>,
4848
tool_choice: Option<serde_json::Value>,
4949
tags: Option<Vec<String>>,
50-
enable_compression: Option<bool>,
51-
compression_rate: Option<f64>,
50+
compression_model: Option<String>,
51+
compression_configuration: Option<CompressionConfiguration>,
5252
}
5353

5454
/// Main client for interacting with the Edgee AI Gateway
@@ -119,11 +119,11 @@ impl Edgee {
119119
if let Some(tags) = parsed.tags {
120120
body["tags"] = json!(tags);
121121
}
122-
if let Some(enable_compression) = parsed.enable_compression {
123-
body["enable_compression"] = json!(enable_compression);
122+
if let Some(compression_model) = &parsed.compression_model {
123+
body["compression_model"] = json!(compression_model);
124124
}
125-
if let Some(compression_rate) = parsed.compression_rate {
126-
body["compression_rate"] = json!(compression_rate);
125+
if let Some(config) = &parsed.compression_configuration {
126+
body["compression_configuration"] = json!(config);
127127
}
128128

129129
let response = self
@@ -198,11 +198,11 @@ impl Edgee {
198198
if let Some(tags) = parsed.tags {
199199
body["tags"] = json!(tags);
200200
}
201-
if let Some(enable_compression) = parsed.enable_compression {
202-
body["enable_compression"] = json!(enable_compression);
201+
if let Some(compression_model) = &parsed.compression_model {
202+
body["compression_model"] = json!(compression_model);
203203
}
204-
if let Some(compression_rate) = parsed.compression_rate {
205-
body["compression_rate"] = json!(compression_rate);
204+
if let Some(config) = &parsed.compression_configuration {
205+
body["compression_configuration"] = json!(config);
206206
}
207207

208208
let response = self
@@ -284,16 +284,16 @@ impl Edgee {
284284
tools: None,
285285
tool_choice: None,
286286
tags: None,
287-
enable_compression: None,
288-
compression_rate: None,
287+
compression_model: None,
288+
compression_configuration: None,
289289
},
290290
Input::Object(obj) => ParsedInput {
291291
messages: obj.messages,
292292
tools: obj.tools,
293293
tool_choice: obj.tool_choice,
294294
tags: obj.tags,
295-
enable_compression: obj.enable_compression,
296-
compression_rate: obj.compression_rate,
295+
compression_model: obj.compression_model,
296+
compression_configuration: obj.compression_configuration,
297297
},
298298
}
299299
}

src/models.rs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,18 @@ pub enum ToolChoice {
183183
},
184184
}
185185

186+
/// Configuration for the compression model.
187+
/// Only relevant for the `agentic` compression model.
188+
#[derive(Debug, Clone, Serialize, Deserialize)]
189+
pub struct CompressionConfiguration {
190+
/// Compression rate (0.0-1.0). Defaults to 0.8 when not specified.
191+
#[serde(skip_serializing_if = "Option::is_none")]
192+
pub rate: Option<f64>,
193+
/// Semantic preservation threshold (0-100).
194+
#[serde(skip_serializing_if = "Option::is_none")]
195+
pub semantic_preservation_threshold: Option<i32>,
196+
}
197+
186198
/// Input for the chat completion request
187199
#[derive(Debug, Clone, Serialize)]
188200
pub struct InputObject {
@@ -193,14 +205,16 @@ pub struct InputObject {
193205
pub tool_choice: Option<serde_json::Value>,
194206
#[serde(skip_serializing_if = "Option::is_none")]
195207
pub tags: Option<Vec<String>>,
196-
/// Enable token compression for this request (overrides API key settings if present)
208+
/// Compression model for this request (agentic, claude, opencode, cursor, or customer).
209+
/// Only one compression model per request. Each model is a bundle of strategies.
197210
/// This is a gateway-internal field and is never sent to providers.
198211
#[serde(default, skip_serializing)]
199-
pub enable_compression: Option<bool>,
200-
/// Compression rate for this request (0.0-1.0, overrides API key settings if present)
212+
pub compression_model: Option<String>,
213+
/// Configuration for the compression model (rate, semantic preservation threshold).
214+
/// Only relevant for the `agentic` compression model.
201215
/// This is a gateway-internal field and is never sent to providers.
202216
#[serde(default, skip_serializing)]
203-
pub compression_rate: Option<f64>,
217+
pub compression_configuration: Option<CompressionConfiguration>,
204218
}
205219

206220
impl InputObject {
@@ -211,8 +225,8 @@ impl InputObject {
211225
tools: None,
212226
tool_choice: None,
213227
tags: None,
214-
enable_compression: None,
215-
compression_rate: None,
228+
compression_model: None,
229+
compression_configuration: None,
216230
}
217231
}
218232

@@ -234,15 +248,15 @@ impl InputObject {
234248
self
235249
}
236250

237-
/// Enable or disable token compression for this request
238-
pub fn with_compression(mut self, enable: bool) -> Self {
239-
self.enable_compression = Some(enable);
251+
/// Set the compression model for this request (agentic, claude, opencode, cursor, customer)
252+
pub fn with_compression_model(mut self, model: impl Into<String>) -> Self {
253+
self.compression_model = Some(model.into());
240254
self
241255
}
242256

243-
/// Set compression rate for this request (0.0-1.0)
244-
pub fn with_compression_rate(mut self, rate: f64) -> Self {
245-
self.compression_rate = Some(rate);
257+
/// Set the compression configuration (only relevant for agentic model)
258+
pub fn with_compression_configuration(mut self, config: CompressionConfiguration) -> Self {
259+
self.compression_configuration = Some(config);
246260
self
247261
}
248262
}
@@ -424,10 +438,15 @@ mod tests {
424438
#[test]
425439
fn test_input_object_with_compression_builder() {
426440
let input = InputObject::new(vec![Message::user("Hello")])
427-
.with_compression(true)
428-
.with_compression_rate(0.5);
429-
430-
assert_eq!(input.enable_compression, Some(true));
431-
assert_eq!(input.compression_rate, Some(0.5));
441+
.with_compression_model("agentic")
442+
.with_compression_configuration(CompressionConfiguration {
443+
rate: Some(0.5),
444+
semantic_preservation_threshold: Some(60),
445+
});
446+
447+
assert_eq!(input.compression_model, Some("agentic".to_string()));
448+
let config = input.compression_configuration.unwrap();
449+
assert_eq!(config.rate, Some(0.5));
450+
assert_eq!(config.semantic_preservation_threshold, Some(60));
432451
}
433452
}

0 commit comments

Comments
 (0)