Skip to main content

asimov_cli/commands/
proxy.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::BoxError;
4use clientele::{StandardOptions, crates::clap::Subcommand};
5
6#[derive(Debug, Subcommand)]
7pub enum ProxyCommand {
8    /// Run an OpenAI-compatible endpoint at <http://127.0.0.1:1920>.
9    ///
10    /// Requests are proxied to enabled providers (currently always OpenRouter).
11    ///
12    /// Reads OPENROUTER_API_KEY for the OpenRouter API key (required).
13    ///
14    /// Reads ASIMOV_PROXY_PORT for the port to bind to (default: 1920).
15    ///
16    /// Reads ASIMOV_PROXY_BIND for the address to bind to (default: 127.0.0.1).
17    ///
18    /// Reads ASIMOV_PROXY_LOG_FILE for a file to append request and
19    /// response bodies to (optional).
20    ///
21    /// Honors the conventional https_proxy/HTTPS_PROXY, all_proxy/ALL_PROXY,
22    /// and no_proxy/NO_PROXY environment variables for reaching upstream
23    /// through an HTTP(S) or SOCKS5 proxy.
24    #[clap(aliases = ["run"])]
25    Serve {
26        #[clap(flatten)]
27        args: ProxyServeArgs,
28    },
29
30    /// Print the proxy URL.
31    #[clap(aliases = ["link"])]
32    Url {},
33
34    /// Print the proxy host.
35    Host {},
36
37    /// Print the proxy port.
38    Port {},
39
40    /// List available models.
41    #[cfg(feature = "unstable")]
42    Models {
43        /// The output format.
44        /// [default: list]
45        /// [possible values: csv, json, list, md, tsv]
46        #[clap(short, long)]
47        format: Option<String>,
48    },
49
50    /// Show configuration for using the proxy endpoint.
51    Config {
52        /// The target application to configure.
53        app: ProxyConfigTarget,
54
55        /// The output format.
56        /// [default: auto]
57        /// [possible values: env, js, json, py, toml, sh, ts]
58        #[clap(short, long)]
59        format: Option<String>,
60    },
61
62    /// Configure applications to use the proxy endpoint.
63    Install {
64        /// The target applications to configure.
65        apps: Vec<ProxyInstallTarget>,
66    },
67}
68
69impl ProxyCommand {
70    pub async fn run(self, flags: &StandardOptions) -> Result<(), BoxError> {
71        use ProxyCommand::*;
72        match self {
73            Serve { args } => serve(args, flags).await,
74            Url {} => url(flags).await,
75            Host {} => host(flags).await,
76            Port {} => port(flags).await,
77            #[cfg(feature = "unstable")]
78            Models { format } => models(format, flags).await,
79            Config { app, format } => config(app, format, flags).await,
80            Install { apps } => install(apps, flags).await,
81        }
82    }
83}
84
85mod config;
86pub use config::*;
87
88mod host;
89pub use host::*;
90
91mod install;
92pub use install::*;
93
94#[cfg(feature = "unstable")]
95mod models;
96#[cfg(feature = "unstable")]
97pub use models::*;
98
99mod port;
100pub use port::*;
101
102mod serve;
103pub use serve::*;
104
105mod url;
106pub use url::*;