extract TokenUsage::from_stream_event helper to keep run_claude under clippy line limit
This commit is contained in:
parent
ce740483c6
commit
4f56954422
2 changed files with 20 additions and 24 deletions
|
|
@ -171,6 +171,24 @@ impl TokenUsage {
|
|||
pub fn context_tokens(&self) -> u64 {
|
||||
self.input_tokens + self.cache_read_input_tokens + self.cache_creation_input_tokens
|
||||
}
|
||||
|
||||
/// Parse usage from a stream-json event. Returns `Some` only for the
|
||||
/// terminal `result` event (which is the only one that carries `usage`);
|
||||
/// every other event maps to `None`. Missing numeric fields default to 0
|
||||
/// so partial server payloads don't drop the whole snapshot.
|
||||
pub fn from_stream_event(v: &serde_json::Value) -> Option<Self> {
|
||||
if v.get("type").and_then(|t| t.as_str()) != Some("result") {
|
||||
return None;
|
||||
}
|
||||
let u = v.get("usage")?;
|
||||
let field = |k: &str| u.get(k).and_then(serde_json::Value::as_u64).unwrap_or(0);
|
||||
Some(Self {
|
||||
input_tokens: field("input_tokens"),
|
||||
output_tokens: field("output_tokens"),
|
||||
cache_read_input_tokens: field("cache_read_input_tokens"),
|
||||
cache_creation_input_tokens: field("cache_creation_input_tokens"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Authoritative turn-loop state. The harness owns it; the web UI
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue