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 {
|
pub fn context_tokens(&self) -> u64 {
|
||||||
self.input_tokens + self.cache_read_input_tokens + self.cache_creation_input_tokens
|
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
|
/// Authoritative turn-loop state. The harness owns it; the web UI
|
||||||
|
|
|
||||||
|
|
@ -277,31 +277,9 @@ async fn run_claude(prompt: &str, files: &TurnFiles, bus: &Bus) -> Result<bool>
|
||||||
}
|
}
|
||||||
match serde_json::from_str::<serde_json::Value>(&line) {
|
match serde_json::from_str::<serde_json::Value>(&line) {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
// Extract token usage from the final `result` event and
|
if let Some(usage) = crate::events::TokenUsage::from_stream_event(&v) {
|
||||||
// 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);
|
bus_out.record_usage(usage);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
bus_out.emit(LiveEvent::Stream(v));
|
bus_out.emit(LiveEvent::Stream(v));
|
||||||
}
|
}
|
||||||
Err(_) => bus_out.emit(LiveEvent::Note(format!("(non-json) {line}"))),
|
Err(_) => bus_out.emit(LiveEvent::Note(format!("(non-json) {line}"))),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue