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
|
|
@ -277,30 +277,8 @@ async fn run_claude(prompt: &str, files: &TurnFiles, bus: &Bus) -> Result<bool>
|
|||
}
|
||||
match serde_json::from_str::<serde_json::Value>(&line) {
|
||||
Ok(v) => {
|
||||
// Extract token usage from the final `result` event and
|
||||
// store it in the bus for the web UI to surface.
|
||||
if v.get("type").and_then(|t| t.as_str()) == Some("result") {
|
||||
if let Some(u) = v.get("usage") {
|
||||
let usage = crate::events::TokenUsage {
|
||||
input_tokens: u
|
||||
.get("input_tokens")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(0),
|
||||
output_tokens: u
|
||||
.get("output_tokens")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(0),
|
||||
cache_read_input_tokens: u
|
||||
.get("cache_read_input_tokens")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(0),
|
||||
cache_creation_input_tokens: u
|
||||
.get("cache_creation_input_tokens")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(0),
|
||||
};
|
||||
bus_out.record_usage(usage);
|
||||
}
|
||||
if let Some(usage) = crate::events::TokenUsage::from_stream_event(&v) {
|
||||
bus_out.record_usage(usage);
|
||||
}
|
||||
bus_out.emit(LiveEvent::Stream(v));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue