feat(tui): add usage report

This commit is contained in:
Felipe Coury
2026-05-21 15:09:14 -03:00
parent a6bedc8a7c
commit afafcc98ec
63 changed files with 2355 additions and 14 deletions
+1
View File
@@ -206,6 +206,7 @@ Example with notification opt-out:
- `plugin/skill/read` — read remote plugin skill markdown on demand by `remoteMarketplaceName`, `remotePluginId`, and `skillName`. This lets clients preview uninstalled remote plugin skills without downloading the plugin bundle.
- `skills/changed` — notification emitted when watched local skill files change.
- `app/list` — list available apps.
- `usage/read` — read forward-only local token usage for a rolling `day` or `week` range, grouped by skills, subagents, apps, MCP servers, and plugins when Codex has tracked those contributors in sqlite.
- `remoteControl/enable` — experimental; enable remote control for the current app-server process and return the current remote-control status snapshot. The caller is responsible for persisting the desired setting outside app-server.
- `remoteControl/disable` — experimental; disable remote control for the current app-server process and return the current remote-control status snapshot. This does not revoke already enrolled controller devices.
- `remoteControl/status/read` — experimental; read the current remote-control status snapshot. `status` is one of `disabled`, `connecting`, `connected`, or `errored`; `serverName` is the local machine name used by this app-server process; `environmentId` is a string when the app-server has a current enrollment and `null` when that enrollment is cleared, invalidated, or remote control is disabled.
+10 -1
View File
@@ -35,6 +35,7 @@ use crate::request_processors::SearchRequestProcessor;
use crate::request_processors::ThreadGoalRequestProcessor;
use crate::request_processors::ThreadRequestProcessor;
use crate::request_processors::TurnRequestProcessor;
use crate::request_processors::UsageRequestProcessor;
use crate::request_processors::WindowsSandboxRequestProcessor;
use crate::request_serialization::QueuedInitializedRequest;
use crate::request_serialization::RequestSerializationQueueKey;
@@ -181,6 +182,7 @@ pub(crate) struct MessageProcessor {
thread_goal_processor: ThreadGoalRequestProcessor,
thread_processor: ThreadRequestProcessor,
turn_processor: TurnRequestProcessor,
usage_processor: UsageRequestProcessor,
windows_sandbox_processor: WindowsSandboxRequestProcessor,
request_serialization_queues: RequestSerializationQueues,
}
@@ -423,9 +425,10 @@ impl MessageProcessor {
thread_watch_manager.clone(),
Arc::clone(&thread_list_state_permit),
thread_goal_processor.clone(),
state_db,
state_db.clone(),
Arc::clone(&skills_watcher),
);
let usage_processor = UsageRequestProcessor::new(state_db);
let turn_processor = TurnRequestProcessor::new(
auth_manager.clone(),
Arc::clone(&thread_manager),
@@ -502,6 +505,7 @@ impl MessageProcessor {
thread_goal_processor,
thread_processor,
turn_processor,
usage_processor,
windows_sandbox_processor,
request_serialization_queues: RequestSerializationQueues::default(),
}
@@ -1115,6 +1119,11 @@ impl MessageProcessor {
ClientRequest::PluginList { params, .. } => {
self.plugin_processor.plugin_list(params).await
}
ClientRequest::UsageRead { params, .. } => self
.usage_processor
.usage_read(params)
.await
.map(|response| Some(response.into())),
ClientRequest::PluginInstalled { params, .. } => {
self.plugin_processor.plugin_installed(params).await
}
@@ -467,6 +467,7 @@ mod search;
mod thread_processor;
mod token_usage_replay;
mod turn_processor;
mod usage_processor;
mod windows_sandbox_processor;
pub(crate) use account_processor::AccountRequestProcessor;
@@ -489,6 +490,7 @@ pub(crate) use search::SearchRequestProcessor;
pub(crate) use thread_goal_processor::ThreadGoalRequestProcessor;
pub(crate) use thread_processor::ThreadRequestProcessor;
pub(crate) use turn_processor::TurnRequestProcessor;
pub(crate) use usage_processor::UsageRequestProcessor;
pub(crate) use windows_sandbox_processor::WindowsSandboxRequestProcessor;
use crate::error_code::internal_error;
@@ -0,0 +1,76 @@
use super::*;
use crate::error_code::internal_error;
use chrono::Utc;
use codex_app_server_protocol::UsageEntry;
use codex_app_server_protocol::UsageHeadline;
use codex_app_server_protocol::UsageRange;
use codex_app_server_protocol::UsageReadParams;
use codex_app_server_protocol::UsageReadResponse;
use codex_app_server_protocol::UsageReport;
use codex_rollout::StateDbHandle;
#[derive(Clone)]
pub(crate) struct UsageRequestProcessor {
state_db: Option<StateDbHandle>,
}
impl UsageRequestProcessor {
pub(crate) fn new(state_db: Option<StateDbHandle>) -> Self {
Self { state_db }
}
pub(crate) async fn usage_read(
&self,
params: UsageReadParams,
) -> Result<UsageReadResponse, JSONRPCErrorError> {
let state_db = self
.state_db
.as_ref()
.ok_or_else(|| internal_error("sqlite state db unavailable for usage"))?;
let report = state_db
.read_usage_report(state_usage_range(params.range), Utc::now().timestamp())
.await
.map_err(|err| internal_error(format!("failed to read usage report: {err}")))?;
Ok(UsageReadResponse {
report: UsageReport {
range: api_usage_range(report.range),
generated_at: report.generated_at,
tracked_from: report.tracked_from,
total_tokens: report.total_tokens,
headline: report.headline.map(|headline| UsageHeadline {
entry: usage_entry(headline.entry),
note: headline.note,
}),
skills: report.skills.into_iter().map(usage_entry).collect(),
subagents: report.subagents.into_iter().map(usage_entry).collect(),
apps: report.apps.into_iter().map(usage_entry).collect(),
mcp_servers: report.mcp_servers.into_iter().map(usage_entry).collect(),
plugins: report.plugins.into_iter().map(usage_entry).collect(),
},
})
}
}
fn usage_entry(entry: codex_state::UsageEntry) -> UsageEntry {
UsageEntry {
kind: entry.kind.into(),
id: entry.id,
label: entry.label,
attributed_tokens: entry.attributed_tokens,
percent_of_usage: entry.percent_of_usage,
}
}
fn state_usage_range(value: UsageRange) -> codex_state::UsageRange {
match value {
UsageRange::Day => codex_state::UsageRange::Day,
UsageRange::Week => codex_state::UsageRange::Week,
}
}
fn api_usage_range(value: codex_state::UsageRange) -> UsageRange {
match value {
codex_state::UsageRange::Day => UsageRange::Day,
codex_state::UsageRange::Week => UsageRange::Week,
}
}