#[derive(Debug, Clone, Default, PartialEq)]
pub struct ResponseMeta {
pub request_id: Option<String>,
pub cost: Option<f64>,
pub cost_status: Option<String>,
pub model: Option<String>,
pub input_tokens: Option<u64>,
pub output_tokens: Option<u64>,
pub total_tokens: Option<u64>,
pub cache_read_tokens: Option<u64>,
pub cache_write_tokens: Option<u64>,
pub limit_source: Option<String>,
pub auth_reason: Option<String>,
pub response_cache: Option<String>,
pub response_cache_age: Option<u64>,
}
pub const HEADER_NAMES: [&str; 13] = [
"x-nr-request-id",
"x-nr-request-cost",
"x-nr-cost-status",
"x-nr-model",
"x-nr-input-tokens",
"x-nr-output-tokens",
"x-nr-total-tokens",
"x-nr-cache-read-tokens",
"x-nr-cache-write-tokens",
"x-nr-limit-source",
"x-nr-auth-reason",
"x-nr-response-cache",
"x-nr-response-cache-age",
];
impl ResponseMeta {
pub fn from_lookup<F>(get: F) -> Self
where
F: Fn(&str) -> Option<String>,
{
let num = |name: &str| get(name).and_then(|v| v.parse::<u64>().ok());
Self {
request_id: get("x-nr-request-id"),
cost: get("x-nr-request-cost").and_then(|v| v.parse::<f64>().ok()),
cost_status: get("x-nr-cost-status"),
model: get("x-nr-model"),
input_tokens: num("x-nr-input-tokens"),
output_tokens: num("x-nr-output-tokens"),
total_tokens: num("x-nr-total-tokens"),
cache_read_tokens: num("x-nr-cache-read-tokens"),
cache_write_tokens: num("x-nr-cache-write-tokens"),
limit_source: get("x-nr-limit-source"),
auth_reason: get("x-nr-auth-reason"),
response_cache: get("x-nr-response-cache"),
response_cache_age: num("x-nr-response-cache-age"),
}
}
pub fn from_headers(headers: &reqwest::header::HeaderMap) -> Self {
Self::from_lookup(|name| {
headers
.get(name)
.and_then(|v| v.to_str().ok())
.map(str::to_owned)
})
}
pub fn is_priced(&self) -> bool {
self.cost_status.as_deref() == Some("exact") && self.cost.is_some()
}
}