story-kit: merge 301_story_dedicated_token_usage_page_in_web_ui

This commit is contained in:
Dave
2026-03-19 11:34:08 +00:00
parent 586d06b840
commit a6ac6497e9
8 changed files with 534 additions and 8 deletions

View File

@@ -130,6 +130,26 @@ struct TokenCostResponse {
agents: Vec<AgentCostEntry>,
}
/// A single token usage record in the all-usage response.
#[derive(Object, Serialize)]
struct TokenUsageRecordResponse {
story_id: String,
agent_name: String,
model: Option<String>,
timestamp: String,
input_tokens: u64,
output_tokens: u64,
cache_creation_input_tokens: u64,
cache_read_input_tokens: u64,
total_cost_usd: f64,
}
/// Response for the all token usage endpoint.
#[derive(Object, Serialize)]
struct AllTokenUsageResponse {
records: Vec<TokenUsageRecordResponse>,
}
/// Returns true if the story file exists in `work/5_done/` or `work/6_archived/`.
///
/// Used to exclude agents for already-archived stories from the `list_agents`
@@ -532,6 +552,42 @@ impl AgentsApi {
agents,
}))
}
/// Get all token usage records across all stories.
///
/// Returns the full history from the persistent token_usage.jsonl log.
#[oai(path = "/token-usage", method = "get")]
async fn get_all_token_usage(
&self,
) -> OpenApiResult<Json<AllTokenUsageResponse>> {
let project_root = self
.ctx
.agents
.get_project_root(&self.ctx.state)
.map_err(bad_request)?;
let records = crate::agents::token_usage::read_all(&project_root)
.map_err(|e| bad_request(format!("Failed to read token usage: {e}")))?;
let response_records: Vec<TokenUsageRecordResponse> = records
.into_iter()
.map(|r| TokenUsageRecordResponse {
story_id: r.story_id,
agent_name: r.agent_name,
model: r.model,
timestamp: r.timestamp,
input_tokens: r.usage.input_tokens,
output_tokens: r.usage.output_tokens,
cache_creation_input_tokens: r.usage.cache_creation_input_tokens,
cache_read_input_tokens: r.usage.cache_read_input_tokens,
total_cost_usd: r.usage.total_cost_usd,
})
.collect();
Ok(Json(AllTokenUsageResponse {
records: response_records,
}))
}
}
#[cfg(test)]