Skip to main content

systemprompt_cli/commands/admin/config/rate_limits/
mod.rs

1//! `admin config rate-limits` command tree: inspect, edit, and compare rate
2//! limits.
3//!
4//! [`RateLimitsCommands`] covers showing the configured limits, setting
5//! endpoint rates, validating the configuration, applying presets, and
6//! importing/exporting/diffing against files or defaults. The editing paths
7//! persist changes to the active profile YAML.
8//!
9//! Copyright (c) systemprompt.io — Business Source License 1.1.
10//! See <https://systemprompt.io> for licensing details.
11
12pub mod diff;
13mod helpers;
14mod import_export;
15pub mod preset;
16mod reset;
17mod set;
18mod show;
19mod validate;
20
21use anyhow::Result;
22use clap::{Args, Subcommand};
23
24use crate::CliConfig;
25use crate::interactive::Prompter;
26use crate::shared::render_result;
27
28#[derive(Debug, Subcommand)]
29pub enum RateLimitsCommands {
30    #[command(about = "Show current rate limits configuration", alias = "list")]
31    Show,
32
33    #[command(about = "Set a rate limit value")]
34    Set(SetArgs),
35
36    #[command(about = "Enable rate limiting")]
37    Enable,
38
39    #[command(about = "Disable rate limiting")]
40    Disable,
41
42    #[command(about = "Validate rate limit configuration")]
43    Validate,
44
45    #[command(about = "Reset rate limits to defaults")]
46    Reset(ResetArgs),
47
48    #[command(subcommand, about = "Manage rate limit presets")]
49    Preset(PresetCommands),
50
51    #[command(about = "Export rate limits to file")]
52    Export(ExportArgs),
53
54    #[command(about = "Import rate limits from file")]
55    Import(ImportArgs),
56
57    #[command(about = "Compare rate limits with defaults or file")]
58    Diff(DiffArgs),
59}
60
61#[derive(Debug, Clone, Args)]
62pub struct SetArgs {
63    #[arg(
64        long,
65        help = "Endpoint to modify: oauth_public, oauth_auth, contexts, tasks, artifacts, \
66                agent_registry, agents, mcp_registry, mcp, stream, content"
67    )]
68    pub endpoint: Option<String>,
69
70    #[arg(long, help = "Rate per second (requires --endpoint)")]
71    pub rate: Option<u64>,
72
73    #[arg(long, help = "Burst multiplier value")]
74    pub burst: Option<u64>,
75}
76
77#[derive(Debug, Clone, Args)]
78pub struct ResetArgs {
79    #[arg(short = 'y', long, help = "Skip confirmation")]
80    pub yes: bool,
81
82    #[arg(long, help = "Preview changes without applying")]
83    pub dry_run: bool,
84
85    #[arg(long, help = "Reset only this endpoint")]
86    pub endpoint: Option<String>,
87}
88
89#[derive(Debug, Subcommand)]
90pub enum PresetCommands {
91    #[command(about = "List available presets")]
92    List,
93
94    #[command(about = "Show preset configuration")]
95    Show(PresetShowArgs),
96
97    #[command(about = "Apply a preset")]
98    Apply(PresetApplyArgs),
99}
100
101#[derive(Debug, Clone, Args)]
102pub struct PresetShowArgs {
103    #[arg(help = "Preset name: development, production, high-traffic")]
104    pub name: String,
105}
106
107#[derive(Debug, Clone, Args)]
108pub struct PresetApplyArgs {
109    #[arg(help = "Preset name: development, production, high-traffic")]
110    pub name: String,
111
112    #[arg(short = 'y', long, help = "Skip confirmation")]
113    pub yes: bool,
114}
115
116#[derive(Debug, Clone, Args)]
117pub struct ExportArgs {
118    #[arg(long, short = 'o', help = "Output file path")]
119    pub output: String,
120
121    #[arg(long, default_value = "yaml", help = "Format: yaml, json")]
122    pub format: String,
123}
124
125#[derive(Debug, Clone, Args)]
126pub struct ImportArgs {
127    #[arg(long, short = 'f', help = "Input file path")]
128    pub file: String,
129
130    #[arg(short = 'y', long, help = "Skip confirmation")]
131    pub yes: bool,
132}
133
134#[derive(Debug, Clone, Args)]
135pub struct DiffArgs {
136    #[arg(long, help = "Compare with defaults")]
137    pub defaults: bool,
138
139    #[arg(long, short = 'f', help = "Compare with file")]
140    pub file: Option<String>,
141}
142
143pub fn execute(
144    command: RateLimitsCommands,
145    prompter: &dyn Prompter,
146    config: &CliConfig,
147) -> Result<()> {
148    match command {
149        RateLimitsCommands::Show => show::execute_show(config),
150        RateLimitsCommands::Set(args) => set::execute_set(&args, config),
151        RateLimitsCommands::Enable => set::execute_enable(config),
152        RateLimitsCommands::Disable => set::execute_disable(config),
153        RateLimitsCommands::Validate => validate::execute_validate(config),
154        RateLimitsCommands::Reset(args) => reset::execute_reset(&args, prompter, config),
155        RateLimitsCommands::Preset(cmd) => preset::execute_preset(cmd, prompter, config),
156        RateLimitsCommands::Export(args) => {
157            let result = import_export::execute_export(&args, config)?;
158            render_result(&result, config);
159            Ok(())
160        },
161        RateLimitsCommands::Import(args) => {
162            let result = import_export::execute_import(&args, prompter, config)?;
163            render_result(&result, config);
164            Ok(())
165        },
166        RateLimitsCommands::Diff(args) => diff::execute_diff(&args, config),
167    }
168}