Repository navigation
Expand file tree
/
Copy pathshared_options.rs
More file actions
220 lines (201 loc) · 7.27 KB
/
Copy pathshared_options.rs
File metadata and controls
220 lines (201 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! Shared command-line flags used by both interactive and non-interactive Codex entry points.
use crate::CliConfigOverrides;
use crate::SandboxModeCliArg;
use clap::Args;
use codex_protocol::config_types::ProfileV2Name;
use std::path::PathBuf;
#[derive(Args, Clone, Debug, Default)]
pub struct SharedCliOptions {
/// Optional image(s) to attach to the initial prompt.
#[arg(
long = "image",
short = 'i',
value_name = "FILE",
value_delimiter = ',',
num_args = 1..
)]
pub images: Vec<PathBuf>,
/// Model the agent should use.
#[arg(long, short = 'm')]
pub model: Option<String>,
/// Use open-source provider.
#[arg(long = "oss", default_value_t = false)]
pub oss: bool,
/// Specify which local provider to use (lmstudio or ollama).
/// If not specified with --oss, will use config default or show selection.
#[arg(long = "local-provider")]
pub oss_provider: Option<String>,
/// Layer $CODEX_HOME/<name>.config.toml on top of the base user config.
#[arg(long = "profile", short = 'p')]
pub config_profile_v2: Option<ProfileV2Name>,
/// Select the sandbox policy to use when executing model-generated shell
/// commands.
#[arg(long = "sandbox", short = 's')]
pub sandbox_mode: Option<SandboxModeCliArg>,
/// Route approval requests through automatic review using the workspace-write sandbox.
#[arg(
long = "approve-for-me",
alias = "not-so-yolo",
default_value_t = false,
conflicts_with_all = ["sandbox_mode", "dangerously_bypass_approvals_and_sandbox"]
)]
pub auto_review: bool,
/// Skip all confirmation prompts and execute commands without sandboxing.
/// EXTREMELY DANGEROUS. Intended solely for running in environments that are externally sandboxed.
#[arg(
long = "dangerously-bypass-approvals-and-sandbox",
alias = "yolo",
default_value_t = false
)]
pub dangerously_bypass_approvals_and_sandbox: bool,
/// Run enabled hooks without requiring persisted hook trust for this invocation.
/// DANGEROUS. Intended only for automation that already vets hook sources.
#[arg(long = "dangerously-bypass-hook-trust", default_value_t = false)]
pub bypass_hook_trust: bool,
/// Tell the agent to use the specified directory as its working root.
#[clap(long = "cd", short = 'C', value_name = "DIR")]
pub cwd: Option<PathBuf>,
/// Run the session in a new managed Git worktree.
#[arg(long = "worktree", default_value_t = false)]
pub worktree: bool,
/// Additional directories that should be writable alongside the primary workspace.
#[arg(long = "add-dir", value_name = "DIR", value_hint = clap::ValueHint::DirPath)]
pub add_dir: Vec<PathBuf>,
}
impl SharedCliOptions {
pub fn take_auto_review_config_overrides(&mut self, overrides: &mut CliConfigOverrides) {
if self.auto_review {
overrides
.raw_overrides
.push(r#"approvals_reviewer="auto_review""#.to_string());
overrides
.raw_overrides
.push(r#"approval_policy="on-request""#.to_string());
overrides
.raw_overrides
.push(r#"sandbox_mode="workspace-write""#.to_string());
self.auto_review = false;
}
}
pub fn inherit_exec_root_options(&mut self, root: &Self) {
let self_selected_sandbox_mode = self.sandbox_mode.is_some()
|| self.auto_review
|| self.dangerously_bypass_approvals_and_sandbox;
let Self {
images,
model,
oss,
oss_provider,
config_profile_v2,
sandbox_mode,
auto_review,
dangerously_bypass_approvals_and_sandbox,
bypass_hook_trust,
cwd,
worktree,
add_dir,
} = self;
let Self {
images: root_images,
model: root_model,
oss: root_oss,
oss_provider: root_oss_provider,
config_profile_v2: root_config_profile_v2,
sandbox_mode: root_sandbox_mode,
auto_review: root_auto_review,
dangerously_bypass_approvals_and_sandbox: root_dangerously_bypass_approvals_and_sandbox,
bypass_hook_trust: root_bypass_hook_trust,
cwd: root_cwd,
worktree: root_worktree,
add_dir: root_add_dir,
} = root;
if model.is_none() {
model.clone_from(root_model);
}
if *root_oss {
*oss = true;
}
if oss_provider.is_none() {
oss_provider.clone_from(root_oss_provider);
}
if config_profile_v2.is_none() {
config_profile_v2.clone_from(root_config_profile_v2);
}
if !self_selected_sandbox_mode {
*sandbox_mode = *root_sandbox_mode;
*auto_review = *root_auto_review;
*dangerously_bypass_approvals_and_sandbox =
*root_dangerously_bypass_approvals_and_sandbox;
}
if !*bypass_hook_trust {
*bypass_hook_trust = *root_bypass_hook_trust;
}
if cwd.is_none() {
cwd.clone_from(root_cwd);
}
*worktree |= *root_worktree;
if !root_images.is_empty() {
let mut merged_images = root_images.clone();
merged_images.append(images);
*images = merged_images;
}
if !root_add_dir.is_empty() {
let mut merged_add_dir = root_add_dir.clone();
merged_add_dir.append(add_dir);
*add_dir = merged_add_dir;
}
}
pub fn apply_subcommand_overrides(&mut self, subcommand: Self) {
let subcommand_selected_sandbox_mode = subcommand.sandbox_mode.is_some()
|| subcommand.auto_review
|| subcommand.dangerously_bypass_approvals_and_sandbox;
let Self {
images,
model,
oss,
oss_provider,
config_profile_v2,
sandbox_mode,
auto_review,
dangerously_bypass_approvals_and_sandbox,
bypass_hook_trust,
cwd,
worktree,
add_dir,
} = subcommand;
if let Some(model) = model {
self.model = Some(model);
}
if oss {
self.oss = true;
}
if let Some(oss_provider) = oss_provider {
self.oss_provider = Some(oss_provider);
}
if let Some(config_profile_v2) = config_profile_v2 {
self.config_profile_v2 = Some(config_profile_v2);
}
if subcommand_selected_sandbox_mode {
self.sandbox_mode = sandbox_mode;
self.auto_review = auto_review;
self.dangerously_bypass_approvals_and_sandbox =
dangerously_bypass_approvals_and_sandbox;
}
if bypass_hook_trust {
self.bypass_hook_trust = true;
}
if let Some(cwd) = cwd {
self.cwd = Some(cwd);
}
self.worktree |= worktree;
if !images.is_empty() {
self.images = images;
}
if !add_dir.is_empty() {
self.add_dir.extend(add_dir);
}
}
}
#[cfg(test)]
#[path = "shared_options_tests.rs"]
mod tests;