cetai 0.1.1

Typed prompt! expressions and #[ai] functions for natural-language tasks over DeepSeek.
Documentation
//! Redacted per-call usage observability via `tracing`.
//!
//! Every completed `prompt!` call emits exactly one INFO event with target
//! `cetai::usage`. The event never contains the API key, the prompt, the
//! parameter JSON, model output or repair content.

use std::time::Duration;

/// Accumulates usage across all logical model requests of one macro call.
#[derive(Default)]
pub(crate) struct UsageStats {
    /// Logical model requests issued (initial generation + repair rounds).
    pub logical_requests: u32,
    /// Physical HTTP retries consumed across all logical requests.
    pub http_retries: u32,
    /// Repair rounds that were entered.
    pub repair_rounds: u32,
    /// Accumulated input tokens, when reported by the API.
    pub input_tokens: Option<u64>,
    /// Accumulated output tokens, when reported by the API.
    pub output_tokens: Option<u64>,
    /// Accumulated cache-hit tokens, when reported by the API.
    pub cache_hit_tokens: Option<u64>,
    /// Accumulated cache-miss tokens, when reported by the API.
    pub cache_miss_tokens: Option<u64>,
    /// Last seen DeepSeek request ID.
    pub request_id: Option<String>,
}

impl UsageStats {
    pub(crate) fn add_usage(&mut self, usage: &crate::deepseek::Usage) {
        accumulate(&mut self.input_tokens, usage.prompt_tokens);
        accumulate(&mut self.output_tokens, usage.completion_tokens);
        accumulate(&mut self.cache_hit_tokens, usage.prompt_cache_hit_tokens);
        accumulate(&mut self.cache_miss_tokens, usage.prompt_cache_miss_tokens);
    }
}

fn accumulate(slot: &mut Option<u64>, value: Option<u64>) {
    if let Some(value) = value {
        *slot = Some(slot.unwrap_or(0) + value);
    }
}

/// Emits the redacted `cetai::usage` event for a finished call.
pub(crate) fn emit(model: Option<&str>, stats: &UsageStats, status: &str, elapsed: Duration) {
    tracing::info!(
        target: "cetai::usage",
        status,
        model = model.unwrap_or("<unresolved>"),
        logical_requests = stats.logical_requests,
        http_retries = stats.http_retries,
        repair_rounds = stats.repair_rounds,
        elapsed_ms = elapsed.as_millis() as u64,
        input_tokens = ?stats.input_tokens,
        output_tokens = ?stats.output_tokens,
        cache_hit_tokens = ?stats.cache_hit_tokens,
        cache_miss_tokens = ?stats.cache_miss_tokens,
        request_id = ?stats.request_id,
        "cetai prompt! call finished",
    );
}