Skip to content

Commit 82daa88

Browse files
author
Greyforge Admin
committed
Respect model option for agent create
1 parent 7954d02 commit 82daa88

4 files changed

Lines changed: 104 additions & 22 deletions

File tree

src/cortex-cli/src/agent_cmd/cli.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ pub struct CreateArgs {
130130
#[arg(short, long, value_name = "DESCRIPTION")]
131131
pub generate: Option<String>,
132132

133-
/// Model to use for AI generation (default: gpt-4o).
134-
#[arg(long, default_value = "gpt-4o")]
135-
pub model: String,
133+
/// Model to use for AI generation or as an agent override.
134+
/// Defaults to gpt-4o when generating with --generate.
135+
#[arg(long)]
136+
pub model: Option<String>,
136137
}
137138

138139
/// Arguments for edit command.

src/cortex-cli/src/agent_cmd/handlers/create.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ pub async fn run_create(args: CreateArgs) -> Result<()> {
4545
}
4646
}
4747

48+
fn model_override_from_input(model_input: &str) -> Option<String> {
49+
let model_input = model_input.trim();
50+
if model_input.is_empty() {
51+
return None;
52+
}
53+
54+
// Issue #2328: Validate model name if provided
55+
match validate_model_name(model_input) {
56+
Ok(valid_model) => Some(valid_model),
57+
Err(e) => {
58+
eprintln!("Warning: {}", e);
59+
eprintln!(
60+
"Using model name as-is. The agent may fail to run if the model doesn't exist."
61+
);
62+
Some(model_input.to_string())
63+
}
64+
}
65+
}
66+
4867
// Only show banner in interactive mode
4968
if !args.non_interactive {
5069
println!("🤖 Cortex Agent Creator");
@@ -220,30 +239,24 @@ pub async fn run_create(args: CreateArgs) -> Result<()> {
220239

221240
// Get optional settings
222241
let (temperature, model, color) = if args.non_interactive {
223-
(None, None, None)
242+
(
243+
None,
244+
model_override_from_input(args.model.as_deref().unwrap_or("")),
245+
None,
246+
)
224247
} else {
225248
println!("\nOptional Settings (press Enter to skip):");
226249

227250
let temp_input = prompt_input(&stdin, &mut stdout, " Temperature (0.0-2.0)", Some(""))?;
228251
let temperature = temp_input.parse::<f32>().ok();
229252

230-
let model_input = prompt_input(&stdin, &mut stdout, " Model override", Some(""))?;
231-
// Issue #2328: Validate model name if provided
232-
let model = if model_input.is_empty() {
233-
None
234-
} else {
235-
// Validate the model name to prevent typos from being accepted
236-
match validate_model_name(&model_input) {
237-
Ok(valid_model) => Some(valid_model),
238-
Err(e) => {
239-
eprintln!("Warning: {}", e);
240-
eprintln!(
241-
"Using model name as-is. The agent may fail to run if the model doesn't exist."
242-
);
243-
Some(model_input)
244-
}
245-
}
246-
};
253+
let model_input = prompt_input(
254+
&stdin,
255+
&mut stdout,
256+
" Model override",
257+
Some(args.model.as_deref().unwrap_or("")),
258+
)?;
259+
let model = model_override_from_input(&model_input);
247260

248261
let color = prompt_input(
249262
&stdin,

src/cortex-cli/src/agent_cmd/handlers/generate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub async fn run_generate(args: CreateArgs) -> Result<()> {
6767
};
6868

6969
// Validate model argument
70-
let model_arg = args.model.trim();
70+
let model_arg = args.model.as_deref().unwrap_or("gpt-4o").trim();
7171
if model_arg.is_empty() {
7272
bail!("Error: Model name cannot be empty");
7373
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use std::fs;
2+
use std::process::Command;
3+
4+
use tempfile::tempdir;
5+
6+
fn combined_output(output: &std::process::Output) -> String {
7+
format!(
8+
"{}{}",
9+
String::from_utf8_lossy(&output.stdout),
10+
String::from_utf8_lossy(&output.stderr)
11+
)
12+
}
13+
14+
#[test]
15+
fn agent_create_non_interactive_writes_requested_model() {
16+
let home = tempdir().unwrap();
17+
let output = Command::new(env!("CARGO_BIN_EXE_Cortex"))
18+
.args([
19+
"agent",
20+
"create",
21+
"--name",
22+
"demo-agent",
23+
"--non-interactive",
24+
"--model",
25+
"claude-sonnet-4-20250514",
26+
])
27+
.env("HOME", home.path())
28+
.env_remove("CORTEX_HOME")
29+
.output()
30+
.unwrap();
31+
32+
assert!(
33+
output.status.success(),
34+
"agent create failed:\n{}",
35+
combined_output(&output)
36+
);
37+
38+
let agent_path = home.path().join(".cortex/agents/demo-agent.md");
39+
let content = fs::read_to_string(&agent_path).unwrap();
40+
assert!(content.contains("model: claude-sonnet-4-20250514"));
41+
}
42+
43+
#[test]
44+
fn agent_create_non_interactive_without_model_omits_model_override() {
45+
let home = tempdir().unwrap();
46+
let output = Command::new(env!("CARGO_BIN_EXE_Cortex"))
47+
.args([
48+
"agent",
49+
"create",
50+
"--name",
51+
"default-agent",
52+
"--non-interactive",
53+
])
54+
.env("HOME", home.path())
55+
.env_remove("CORTEX_HOME")
56+
.output()
57+
.unwrap();
58+
59+
assert!(
60+
output.status.success(),
61+
"agent create failed:\n{}",
62+
combined_output(&output)
63+
);
64+
65+
let agent_path = home.path().join(".cortex/agents/default-agent.md");
66+
let content = fs::read_to_string(&agent_path).unwrap();
67+
assert!(!content.contains("model:"));
68+
}

0 commit comments

Comments
 (0)