Skip to main content

tea_cli/
args.rs

1use std::path::PathBuf;
2use std::str::FromStr;
3
4use clap::{ArgAction, Parser, ValueEnum};
5use tea_protocol::{ProfileId, ReasoningEffort, SessionId};
6use tea_provider_openai::ApiKey;
7
8/// Stable non-interactive project trust selection.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
10pub enum TrustArg {
11    /// Use persisted/default policy.
12    Default,
13    /// Trust project-local resources for this invocation.
14    Once,
15    /// Trust and persist the canonical workspace decision.
16    Persist,
17    /// Reject project-local resources and fail.
18    Reject,
19    /// Ignore project-local resources.
20    Ignore,
21}
22
23/// Durable session selection shared by headless and future interactive modes.
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum SessionSelection {
26    /// Create a new durable session.
27    New,
28    /// Continue the most recently updated durable session.
29    Continue,
30    /// Open one explicit durable session.
31    Existing(SessionId),
32    /// Use an invocation-local in-memory `SQLite` session.
33    NoSession,
34}
35
36/// Shared command-line contract for the Coding CLI.
37#[derive(Debug, Clone, Parser)]
38#[command(name = "tea", version = crate::version())]
39#[allow(clippy::struct_excessive_bools)] // Clap presence flags map to one validated selection.
40pub struct CliArgs {
41    /// Run one script-safe prompt and print only the final assistant text.
42    #[arg(long, conflicts_with_all = ["json", "rpc"])]
43    pub print: bool,
44    /// Stream one mode header and canonical event envelope per JSON/LF line.
45    #[arg(long, conflicts_with_all = ["print", "rpc"])]
46    pub json: bool,
47    /// Run the strict bounded LF-delimited JSON/RPC process interface.
48    #[arg(long, conflicts_with_all = ["print", "json"])]
49    pub rpc: bool,
50    /// Workspace directory (defaults to the process current directory).
51    #[arg(long, value_name = "DIR")]
52    pub cwd: Option<PathBuf>,
53    /// Provider selector.
54    #[arg(long)]
55    pub provider: Option<String>,
56    /// Model selector.
57    #[arg(long)]
58    pub model: Option<String>,
59    /// Invocation-local provider-neutral reasoning effort.
60    #[arg(long)]
61    pub reasoning_effort: Option<ReasoningEffort>,
62    /// Invocation-local provider API key override.
63    #[arg(long, value_name = "SECRET")]
64    pub api_key: Option<ApiKey>,
65    /// Product profile selector.
66    #[arg(long, default_value = "coding-agent")]
67    pub profile: String,
68    /// Active tools, as comma-separated names. Repeatable.
69    #[arg(long, value_delimiter = ',', action = ArgAction::Append)]
70    pub tools: Vec<String>,
71    /// Additional workspace-relative context file. Repeatable.
72    #[arg(long = "context-file", value_name = "PATH")]
73    pub context_files: Vec<String>,
74    /// Open one durable session ID.
75    #[arg(long, conflicts_with_all = ["new_session", "continue_session", "no_session"])]
76    pub session: Option<String>,
77    /// Force a new durable session.
78    #[arg(long = "new", conflicts_with_all = ["session", "continue_session", "no_session"])]
79    pub new_session: bool,
80    /// Continue the most recently updated durable session.
81    #[arg(long = "continue", conflicts_with_all = ["session", "new_session", "no_session"])]
82    pub continue_session: bool,
83    /// Do not persist this invocation's session.
84    #[arg(long, conflicts_with_all = ["session", "new_session", "continue_session"])]
85    pub no_session: bool,
86    /// Absolute application configuration directory.
87    #[arg(long, value_name = "DIR")]
88    pub config_dir: Option<PathBuf>,
89    /// Absolute durable state directory.
90    #[arg(long, value_name = "DIR")]
91    pub state_dir: Option<PathBuf>,
92    /// Absolute declarative resource directory.
93    #[arg(long, value_name = "DIR")]
94    pub data_dir: Option<PathBuf>,
95    /// Explicit `SQLite` path; relative values are resolved beneath state-dir.
96    #[arg(long, value_name = "PATH")]
97    pub session_db: Option<PathBuf>,
98    /// Project-local configuration/resource trust behavior.
99    #[arg(long, value_enum, default_value = "default")]
100    pub trust: TrustArg,
101    /// Increase diagnostic verbosity (stdout remains machine-safe in headless modes).
102    #[arg(short, long, action = ArgAction::Count)]
103    pub verbose: u8,
104    /// Initial prompt text; values beginning with @ load a workspace-relative file.
105    #[arg(value_name = "PROMPT")]
106    pub prompt: Vec<String>,
107}
108
109impl CliArgs {
110    /// Resolves and validates the requested session behavior.
111    ///
112    /// # Errors
113    ///
114    /// Rejects malformed explicit session IDs.
115    pub fn session_selection(&self) -> Result<SessionSelection, crate::CliFailure> {
116        if let Some(session) = &self.session {
117            return SessionId::from_str(session)
118                .map(SessionSelection::Existing)
119                .map_err(|_| crate::CliFailure::usage("session identifier is invalid"));
120        }
121        if self.continue_session {
122            Ok(SessionSelection::Continue)
123        } else if self.no_session {
124            Ok(SessionSelection::NoSession)
125        } else {
126            Ok(SessionSelection::New)
127        }
128    }
129
130    /// Resolves the registered profile selector.
131    ///
132    /// # Errors
133    ///
134    /// Rejects malformed profile IDs.
135    pub fn profile_id(&self) -> Result<ProfileId, crate::CliFailure> {
136        ProfileId::from_str(&self.profile)
137            .map_err(|_| crate::CliFailure::usage("profile selector is invalid"))
138    }
139}