use std::path::PathBuf;
use std::process::ExitCode;
use clap::{ArgGroup, Args, Subcommand};
use crate::error::CliError;
#[derive(Debug, Clone, Args)]
pub(crate) struct PricingCommand {
#[command(subcommand)]
pub(crate) command: PricingSubcommand,
}
#[derive(Debug, Clone, Subcommand)]
pub(crate) enum PricingSubcommand {
Validate(PricingValidateCommand),
Init(PricingInitCommand),
AddSource(PricingAddSourceCommand),
Resolve(PricingResolveCommand),
}
#[derive(Debug, Clone, Default, Args)]
#[command(group(
ArgGroup::new("pricing_scope")
.args(["user", "project", "global"])
.multiple(false)
))]
pub(crate) struct PricingScopeArgs {
#[arg(long)]
pub(crate) user: bool,
#[arg(long)]
pub(crate) project: bool,
#[arg(long)]
pub(crate) global: bool,
}
#[derive(Debug, Clone, Args)]
pub(crate) struct PricingValidateCommand {
pub(crate) path: PathBuf,
}
#[derive(Debug, Clone, Args)]
pub(crate) struct PricingInitCommand {
#[command(flatten)]
pub(crate) scope: PricingScopeArgs,
}
#[derive(Debug, Clone, Args)]
pub(crate) struct PricingAddSourceCommand {
#[command(flatten)]
pub(crate) scope: PricingScopeArgs,
pub(crate) path: PathBuf,
#[arg(long)]
pub(crate) append: bool,
}
#[derive(Debug, Clone, Args)]
pub(crate) struct PricingResolveCommand {
pub(crate) model: String,
#[arg(long)]
pub(crate) provider: Option<String>,
#[arg(long)]
pub(crate) prompt_tokens: Option<u64>,
#[arg(long)]
pub(crate) completion_tokens: Option<u64>,
#[arg(long)]
pub(crate) cache_read_tokens: Option<u64>,
#[arg(long)]
pub(crate) cache_write_tokens: Option<u64>,
}
impl From<PricingScopeArgs> for crate::plugins::ConfigurationScope {
fn from(value: PricingScopeArgs) -> Self {
match (value.user, value.project, value.global) {
(false, false, false) => Self::Default,
(true, false, false) => Self::User,
(false, true, false) => Self::Project,
(false, false, true) => Self::Global,
_ => Self::Invalid,
}
}
}
impl PricingValidateCommand {
pub(crate) fn into_runtime(self) -> crate::plugins::PricingValidateRequest {
crate::plugins::PricingValidateRequest { path: self.path }
}
}
impl PricingInitCommand {
pub(crate) fn into_runtime(self) -> crate::plugins::PricingInitRequest {
crate::plugins::PricingInitRequest {
scope: self.scope.into(),
}
}
}
impl PricingAddSourceCommand {
pub(crate) fn into_runtime(self) -> crate::plugins::PricingAddSourceRequest {
crate::plugins::PricingAddSourceRequest {
scope: self.scope.into(),
path: self.path,
append: self.append,
}
}
}
impl PricingResolveCommand {
pub(crate) fn into_runtime(self) -> crate::plugins::PricingResolveRequest {
crate::plugins::PricingResolveRequest {
model: self.model,
provider: self.provider,
prompt_tokens: self.prompt_tokens,
completion_tokens: self.completion_tokens,
cache_read_tokens: self.cache_read_tokens,
cache_write_tokens: self.cache_write_tokens,
}
}
}
pub(super) fn execute(command: PricingCommand) -> Result<ExitCode, CliError> {
match command.command {
PricingSubcommand::Validate(command) => {
crate::plugins::pricing::validate(command.into_runtime())?
}
PricingSubcommand::Init(command) => crate::plugins::pricing::init(command.into_runtime())?,
PricingSubcommand::AddSource(command) => {
crate::plugins::pricing::add_source(command.into_runtime())?
}
PricingSubcommand::Resolve(command) => {
crate::plugins::pricing::resolve(command.into_runtime())?
}
}
Ok(ExitCode::SUCCESS)
}