mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
feat(app-server): expose usage report
This commit is contained in:
@@ -36,6 +36,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;
|
||||
@@ -182,6 +183,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,
|
||||
}
|
||||
@@ -428,9 +430,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),
|
||||
@@ -507,6 +510,7 @@ impl MessageProcessor {
|
||||
thread_goal_processor,
|
||||
thread_processor,
|
||||
turn_processor,
|
||||
usage_processor,
|
||||
windows_sandbox_processor,
|
||||
request_serialization_queues: RequestSerializationQueues::default(),
|
||||
}
|
||||
@@ -1123,6 +1127,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
|
||||
}
|
||||
|
||||
@@ -474,6 +474,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;
|
||||
@@ -496,6 +497,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,77 @@
|
||||
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(),
|
||||
agent_tasks: report.agent_tasks.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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user