Skip to content

Commit 0cc1b7a

Browse files
committed
suppert --model
1 parent e58ea07 commit 0cc1b7a

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

crates/chat-cli/src/cli/chat/cli.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub struct Chat {
2525
/// Context profile to use
2626
#[arg(long = "profile")]
2727
pub profile: Option<String>,
28+
/// Current model to use
29+
#[arg(long = "model")]
30+
pub model: Option<String>,
2831
/// Allows the model to use any tool to run commands without asking for confirmation.
2932
#[arg(long)]
3033
pub trust_all_tools: bool,

crates/chat-cli/src/cli/chat/mod.rs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ const WELCOME_TEXT: &str = color_print::cstr! {"<cyan!>
215215
const SMALL_SCREEN_WELCOME_TEXT: &str = color_print::cstr! {"<em>Welcome to <cyan!>Amazon Q</cyan!>!</em>"};
216216
const RESUME_TEXT: &str = color_print::cstr! {"<em>Picking up where we left off...</em>"};
217217

218-
const ROTATING_TIPS: [&str; 13] = [
218+
const ROTATING_TIPS: [&str; 14] = [
219219
color_print::cstr! {"You can resume the last conversation from your current directory by launching with <green!>q chat --resume</green!>"},
220220
color_print::cstr! {"Get notified whenever Q CLI finishes responding. Just run <green!>q settings chat.enableNotifications true</green!>"},
221221
color_print::cstr! {"You can use <green!>/editor</green!> to edit your prompt with a vim-like experience"},
@@ -229,6 +229,7 @@ const ROTATING_TIPS: [&str; 13] = [
229229
color_print::cstr! {"You can enable custom tools with <green!>MCP servers</green!>. Learn more with /help"},
230230
color_print::cstr! {"You can specify wait time (in ms) for mcp server loading with <green!>q settings mcp.initTimeout {timeout in int}</green!>. Servers that takes longer than the specified time will continue to load in the background. Use /tools to see pending servers."},
231231
color_print::cstr! {"You can see the server load status as well as any warnings or errors associated with <green!>/mcp</green!>"},
232+
color_print::cstr! {"Customize your current chat session by choosing a model with <green!>/model</green!>"},
232233
];
233234

234235
const GREETING_BREAK_POINT: usize = 80;
@@ -265,6 +266,7 @@ const HELP_TEXT: &str = color_print::cstr! {"
265266
<em>trustall</em> <black!>Trust all tools (equivalent to deprecated /acceptall)</black!>
266267
<em>reset</em> <black!>Reset all tools to default permission levels</black!>
267268
<em>/mcp</em> <black!>See mcp server loaded</black!>
269+
<em>/model</em> <black!>Select a model for the current conversation session</black!>
268270
<em>/profile</em> <black!>Manage profiles</black!>
269271
<em>help</em> <black!>Show profile help</black!>
270272
<em>list</em> <black!>List profiles</black!>
@@ -324,6 +326,7 @@ pub async fn launch_chat(database: &mut Database, telemetry: &TelemetryThread, a
324326
args.resume,
325327
args.accept_all,
326328
args.profile,
329+
args.model,
327330
args.trust_all_tools,
328331
trust_tools,
329332
)
@@ -339,6 +342,7 @@ pub async fn chat(
339342
resume_conversation: bool,
340343
accept_all: bool,
341344
profile: Option<String>,
345+
model_name: Option<String>,
342346
trust_all_tools: bool,
343347
trust_tools: Option<Vec<String>>,
344348
) -> Result<ExitCode> {
@@ -415,6 +419,29 @@ pub async fn chat(
415419
}
416420
}
417421

422+
// If modelId is specified, verify it exists before starting the chat
423+
let model_id: Option<String> = if let Some(model_name) = model_name {
424+
match MODEL_OPTIONS.iter().find(|(name, _)| *name == model_name) {
425+
Some((name, id)) => {
426+
if *name == "Auto" {
427+
None // Auto maps to None
428+
} else {
429+
Some(id.to_string())
430+
}
431+
},
432+
None => {
433+
let available_names: Vec<&str> = MODEL_OPTIONS.iter().map(|(name, _)| *name).collect();
434+
bail!(
435+
"Model '{}' does not exist. Available models: {}",
436+
model_name,
437+
available_names.join(", ")
438+
);
439+
},
440+
}
441+
} else {
442+
None
443+
};
444+
418445
let conversation_id = Alphanumeric.sample_string(&mut rand::rng(), 9);
419446
info!(?conversation_id, "Generated new conversation id");
420447
let (prompt_request_sender, prompt_request_receiver) = std::sync::mpsc::channel::<Option<String>>();
@@ -473,6 +500,7 @@ pub async fn chat(
473500
|| terminal::window_size().map(|s| s.columns.into()).ok(),
474501
tool_manager,
475502
profile,
503+
model_id,
476504
tool_config,
477505
tool_permissions,
478506
)
@@ -560,16 +588,20 @@ impl ChatContext {
560588
terminal_width_provider: fn() -> Option<usize>,
561589
tool_manager: ToolManager,
562590
profile: Option<String>,
591+
model_id: Option<String>,
563592
tool_config: HashMap<String, ToolSpec>,
564593
tool_permissions: ToolPermissions,
565594
) -> Result<Self> {
566595
let ctx_clone = Arc::clone(&ctx);
567596
let output_clone = output.clone();
568597

569598
let mut existing_conversation = false;
570-
let model_id = match database.get_last_used_model_id() {
571-
Ok(Some(id)) => Some(id),
572-
Ok(None) | Err(_) => database.settings.get_string(Setting::UserDefaultModel),
599+
let valid_model_id = match model_id {
600+
Some(id) => Some(id),
601+
None => match database.get_last_used_model_id() {
602+
Ok(Some(id)) => Some(id),
603+
Ok(None) | Err(_) => database.settings.get_string(Setting::UserDefaultModel),
604+
},
573605
};
574606
let conversation_state = if resume_conversation {
575607
let prior = std::env::current_dir()
@@ -597,7 +629,7 @@ impl ChatContext {
597629
profile,
598630
Some(output_clone),
599631
tool_manager,
600-
model_id,
632+
valid_model_id,
601633
)
602634
.await
603635
}
@@ -609,7 +641,7 @@ impl ChatContext {
609641
profile,
610642
Some(output_clone),
611643
tool_manager,
612-
model_id,
644+
valid_model_id,
613645
)
614646
.await
615647
};
@@ -4007,6 +4039,7 @@ mod tests {
40074039
|| Some(80),
40084040
tool_manager,
40094041
None,
4042+
None,
40104043
tool_config,
40114044
ToolPermissions::new(0),
40124045
)
@@ -4153,6 +4186,7 @@ mod tests {
41534186
|| Some(80),
41544187
tool_manager,
41554188
None,
4189+
None,
41564190
tool_config,
41574191
ToolPermissions::new(0),
41584192
)
@@ -4252,6 +4286,7 @@ mod tests {
42524286
|| Some(80),
42534287
tool_manager,
42544288
None,
4289+
None,
42554290
tool_config,
42564291
ToolPermissions::new(0),
42574292
)
@@ -4330,6 +4365,7 @@ mod tests {
43304365
|| Some(80),
43314366
tool_manager,
43324367
None,
4368+
None,
43334369
tool_config,
43344370
ToolPermissions::new(0),
43354371
)

crates/chat-cli/src/cli/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ mod test {
372372
resume: false,
373373
input: None,
374374
profile: None,
375+
model: None,
375376
trust_all_tools: false,
376377
trust_tools: None,
377378
})),
@@ -411,6 +412,7 @@ mod test {
411412
resume: false,
412413
input: None,
413414
profile: Some("my-profile".to_string()),
415+
model: None,
414416
trust_all_tools: false,
415417
trust_tools: None,
416418
})
@@ -427,6 +429,7 @@ mod test {
427429
resume: false,
428430
input: Some("Hello".to_string()),
429431
profile: Some("my-profile".to_string()),
432+
model: None,
430433
trust_all_tools: false,
431434
trust_tools: None,
432435
})
@@ -443,6 +446,7 @@ mod test {
443446
resume: false,
444447
input: None,
445448
profile: Some("my-profile".to_string()),
449+
model: None,
446450
trust_all_tools: false,
447451
trust_tools: None,
448452
})
@@ -459,6 +463,7 @@ mod test {
459463
resume: true,
460464
input: None,
461465
profile: None,
466+
model: None,
462467
trust_all_tools: false,
463468
trust_tools: None,
464469
})
@@ -471,6 +476,7 @@ mod test {
471476
resume: true,
472477
input: None,
473478
profile: None,
479+
model: None,
474480
trust_all_tools: false,
475481
trust_tools: None,
476482
})
@@ -487,6 +493,7 @@ mod test {
487493
resume: false,
488494
input: None,
489495
profile: None,
496+
model: None,
490497
trust_all_tools: true,
491498
trust_tools: None,
492499
})
@@ -503,6 +510,7 @@ mod test {
503510
resume: false,
504511
input: None,
505512
profile: None,
513+
model: None,
506514
trust_all_tools: false,
507515
trust_tools: Some(vec!["".to_string()]),
508516
})
@@ -519,6 +527,7 @@ mod test {
519527
resume: false,
520528
input: None,
521529
profile: None,
530+
model: None,
522531
trust_all_tools: false,
523532
trust_tools: Some(vec!["fs_read".to_string(), "fs_write".to_string()]),
524533
})

0 commit comments

Comments
 (0)