#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Role {
#[serde(alias = "System", alias = "developer")]
System,
#[serde(alias = "User")]
User,
#[serde(alias = "Assistant")]
Assistant,
#[serde(alias = "Tool")]
Tool,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ChatMessage {
pub role: Role,
pub content: String,
}
impl ChatMessage {
pub fn new(role: Role, content: impl Into<String>) -> Self {
Self {
role,
content: content.into(),
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ChatRequest {
pub model: String,
pub messages: Vec<ChatMessage>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_tokens: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub temperature: Option<f32>,
}
impl ChatRequest {
pub fn new(model: impl Into<String>, messages: Vec<ChatMessage>) -> Self {
Self {
model: model.into(),
messages,
max_tokens: None,
temperature: None,
}
}
pub fn total_content_chars(&self) -> usize {
self.messages
.iter()
.map(|m| m.content.chars().count())
.sum()
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ChatResponse {
pub content: String,
pub model: String,
pub input_tokens: u32,
pub output_tokens: u32,
}
impl ChatResponse {
pub fn usage(&self) -> Usage {
Usage::new(u64::from(self.input_tokens), u64::from(self.output_tokens))
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[non_exhaustive]
pub struct Usage {
pub input: u64,
pub output: u64,
pub cache_read: u64,
pub cache_write: u64,
pub reasoning: u64,
}
impl Usage {
pub fn new(input: u64, output: u64) -> Self {
Self {
input,
output,
..Self::default()
}
}
pub fn with_input(mut self, tokens: u64) -> Self {
self.input = tokens;
self
}
pub fn with_output(mut self, tokens: u64) -> Self {
self.output = tokens;
self
}
pub fn with_cache_read(mut self, tokens: u64) -> Self {
self.cache_read = tokens;
self
}
pub fn with_cache_write(mut self, tokens: u64) -> Self {
self.cache_write = tokens;
self
}
pub fn with_reasoning(mut self, tokens: u64) -> Self {
self.reasoning = tokens;
self
}
pub fn total_input(&self) -> u64 {
self.input
.saturating_add(self.cache_read)
.saturating_add(self.cache_write)
}
pub fn total_output(&self) -> u64 {
self.output.saturating_add(self.reasoning)
}
pub fn total(&self) -> u64 {
self.total_input().saturating_add(self.total_output())
}
pub fn from_json(json: &serde_json::Value) -> Option<Usage> {
let u = json.get("usage").filter(|u| u.is_object()).unwrap_or(json);
let n = |v: Option<&serde_json::Value>| v.and_then(serde_json::Value::as_u64).unwrap_or(0);
let at = |path: &[&str]| n(path.iter().try_fold(u, |v, k| v.get(k)));
if u.get("prompt_tokens").is_some() || u.get("completion_tokens").is_some() {
let prompt = at(&["prompt_tokens"]);
let completion = at(&["completion_tokens"]);
let cached = at(&["prompt_tokens_details", "cached_tokens"])
.max(at(&["prompt_cache_hit_tokens"]))
.min(prompt);
let reasoning = at(&["completion_tokens_details", "reasoning_tokens"]).min(completion);
return Some(
Usage::new(prompt - cached, completion - reasoning)
.with_cache_read(cached)
.with_reasoning(reasoning),
);
}
if u.get("input_tokens").is_none() && u.get("output_tokens").is_none() {
return None;
}
let input = at(&["input_tokens"]);
let output = at(&["output_tokens"]);
if u.get("input_tokens_details").is_some() || u.get("output_tokens_details").is_some() {
let cached = at(&["input_tokens_details", "cached_tokens"]).min(input);
let reasoning = at(&["output_tokens_details", "reasoning_tokens"]).min(output);
return Some(
Usage::new(input - cached, output - reasoning)
.with_cache_read(cached)
.with_reasoning(reasoning),
);
}
Some(
Usage::new(input, output)
.with_cache_read(at(&["cache_read_input_tokens"]))
.with_cache_write(at(&["cache_creation_input_tokens"])),
)
}
}
impl std::ops::Add for Usage {
type Output = Usage;
fn add(self, rhs: Usage) -> Usage {
Usage {
input: self.input.saturating_add(rhs.input),
output: self.output.saturating_add(rhs.output),
cache_read: self.cache_read.saturating_add(rhs.cache_read),
cache_write: self.cache_write.saturating_add(rhs.cache_write),
reasoning: self.reasoning.saturating_add(rhs.reasoning),
}
}
}
impl std::ops::AddAssign for Usage {
fn add_assign(&mut self, rhs: Usage) {
*self = *self + rhs;
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct StreamChunk {
pub delta: String,
pub finished: bool,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_chat_message_new() {
let m = ChatMessage::new(Role::User, "hello");
assert_eq!(m.role, Role::User);
assert_eq!(m.content, "hello");
}
#[test]
fn test_chat_request_total_content_chars() {
let req = ChatRequest::new(
"model",
vec![
ChatMessage::new(Role::User, "hello"),
ChatMessage::new(Role::Assistant, "world!"),
],
);
assert_eq!(req.total_content_chars(), 11);
}
#[test]
fn test_role_serialization_roundtrip() {
let r = Role::System;
let json = serde_json::to_string(&r).unwrap();
let back: Role = serde_json::from_str(&json).unwrap();
assert_eq!(back, r);
}
#[test]
fn test_chat_response_fields() {
let r = ChatResponse {
content: "hi".into(),
model: "gpt-4o".into(),
input_tokens: 10,
output_tokens: 5,
};
assert_eq!(r.input_tokens + r.output_tokens, 15);
}
#[test]
fn test_usage_builders_totals_and_sum() {
let u = Usage::new(10, 5)
.with_cache_read(100)
.with_cache_write(20)
.with_reasoning(7);
assert_eq!(
(u.total_input(), u.total_output(), u.total()),
(130, 12, 142)
);
let mut sum = Usage::default();
sum += u;
sum += Usage::new(1, 1);
assert_eq!(
sum,
Usage::new(11, 6)
.with_cache_read(100)
.with_cache_write(20)
.with_reasoning(7)
);
assert_eq!((Usage::new(u64::MAX, 0) + Usage::new(1, 0)).input, u64::MAX);
let back: Usage = serde_json::from_str(r#"{"input":3,"cache_read":4}"#).unwrap();
assert_eq!(back, Usage::new(3, 0).with_cache_read(4));
let r = ChatResponse {
content: String::new(),
model: "m".into(),
input_tokens: 9,
output_tokens: 2,
};
assert_eq!(r.usage(), Usage::new(9, 2));
}
#[test]
fn test_usage_from_json_shapes() {
use serde_json::json;
let a = json!({"usage": {"input_tokens": 30, "output_tokens": 9,
"cache_read_input_tokens": 1_000, "cache_creation_input_tokens": 200}});
assert_eq!(
Usage::from_json(&a),
Some(
Usage::new(30, 9)
.with_cache_read(1_000)
.with_cache_write(200)
)
);
let r = json!({"input_tokens": 1_000, "output_tokens": 50,
"input_tokens_details": {"cached_tokens": 800}, "output_tokens_details": {"reasoning_tokens": 20}});
assert_eq!(
Usage::from_json(&r),
Some(Usage::new(200, 30).with_cache_read(800).with_reasoning(20))
);
let d =
json!({"prompt_tokens": 100, "completion_tokens": 5, "prompt_cache_hit_tokens": 60});
assert_eq!(
Usage::from_json(&d),
Some(Usage::new(40, 5).with_cache_read(60))
);
let bad = json!({"prompt_tokens": 1, "completion_tokens": "x", "prompt_tokens_details": {"cached_tokens": 9}});
assert_eq!(
Usage::from_json(&bad),
Some(Usage::new(0, 0).with_cache_read(1))
);
assert_eq!(Usage::from_json(&json!({"choices": []})), None);
assert_eq!(Usage::from_json(&json!(null)), None);
assert_eq!(Usage::from_json(&json!({"usage": null})), None);
}
#[test]
fn test_stream_chunk_finished_flag() {
let chunk = StreamChunk {
delta: "done".into(),
finished: true,
};
assert!(chunk.finished);
}
#[test]
fn test_request_serializes_to_openai_chat_body() {
let req = ChatRequest::new(
"gpt-4o-mini",
vec![
ChatMessage::new(Role::System, "Be brief."),
ChatMessage::new(Role::User, "Hi"),
],
);
let body = serde_json::to_value(&req).unwrap();
assert_eq!(
body,
serde_json::json!({"model": "gpt-4o-mini", "messages": [
{"role": "system", "content": "Be brief."}, {"role": "user", "content": "Hi"}]})
);
let old: ChatRequest = serde_json::from_str(
r#"{"model":"m","messages":[{"role":"User","content":"a"},{"role":"developer","content":"b"}],"max_tokens":null,"temperature":null}"#,
).unwrap();
assert_eq!(old.messages[0].role, Role::User);
assert_eq!(old.messages[1].role, Role::System);
}
}