@@ -15,7 +15,10 @@ use std::path::{
15
15
} ;
16
16
17
17
use context_migrate:: ContextMigrate ;
18
- use crossterm:: style:: Stylize as _;
18
+ use crossterm:: style:: {
19
+ Color ,
20
+ Stylize as _,
21
+ } ;
19
22
use crossterm:: {
20
23
queue,
21
24
style,
@@ -47,6 +50,7 @@ use super::chat::tools::{
47
50
NATIVE_TOOLS ,
48
51
ToolOrigin ,
49
52
} ;
53
+ use crate :: database:: settings:: Setting ;
50
54
use crate :: os:: Os ;
51
55
use crate :: util:: {
52
56
MCP_SERVER_TOOL_DELIMITER ,
@@ -432,11 +436,51 @@ impl Agents {
432
436
433
437
local_agents. append ( & mut global_agents) ;
434
438
435
- // If we are told which agent to set as active, we will fall back to a default whose
436
- // lifetime matches that of the session
437
- if agent_name. is_none ( ) {
439
+ // Assume agent in the following order of priority:
440
+ // 1. The agent name specified by the start command via --agent (this is the agent_name that's
441
+ // passed in)
442
+ // 2. If the above is missing or invalid, assume one that is specified by chat.defaultAgent
443
+ // 3. If the above is missing or invalid, assume the in-memory default
444
+ let active_idx = ' active_idx: {
445
+ if let Some ( name) = agent_name {
446
+ if local_agents. iter ( ) . any ( |a| a. name . as_str ( ) == name) {
447
+ break ' active_idx name. to_string ( ) ;
448
+ }
449
+ let _ = queue ! (
450
+ output,
451
+ style:: SetForegroundColor ( Color :: Red ) ,
452
+ style:: Print ( "Error" ) ,
453
+ style:: SetForegroundColor ( Color :: Yellow ) ,
454
+ style:: Print ( format!(
455
+ ": no agent with name {} found. Falling back to user specified default" ,
456
+ name
457
+ ) ) ,
458
+ style:: Print ( "\n " ) ,
459
+ style:: SetForegroundColor ( Color :: Reset )
460
+ ) ;
461
+ }
462
+
463
+ if let Some ( user_set_default) = os. database . settings . get_string ( Setting :: ChatDefaultAgent ) {
464
+ if local_agents. iter ( ) . any ( |a| a. name == user_set_default) {
465
+ break ' active_idx user_set_default;
466
+ }
467
+ let _ = queue ! (
468
+ output,
469
+ style:: SetForegroundColor ( Color :: Red ) ,
470
+ style:: Print ( "Error" ) ,
471
+ style:: SetForegroundColor ( Color :: Yellow ) ,
472
+ style:: Print ( format!(
473
+ ": user defined default {} not found. Falling back to in-memory default" ,
474
+ user_set_default
475
+ ) ) ,
476
+ style:: Print ( "\n " ) ,
477
+ style:: SetForegroundColor ( Color :: Reset )
478
+ ) ;
479
+ }
480
+
438
481
local_agents. push ( Agent :: default ( ) ) ;
439
- }
482
+ "default" . to_string ( )
483
+ } ;
440
484
441
485
let _ = output. flush ( ) ;
442
486
@@ -445,7 +489,7 @@ impl Agents {
445
489
. into_iter ( )
446
490
. map ( |a| ( a. name . clone ( ) , a) )
447
491
. collect :: < HashMap < _ , _ > > ( ) ,
448
- active_idx : agent_name . unwrap_or ( "default" ) . to_string ( ) ,
492
+ active_idx,
449
493
..Default :: default ( )
450
494
}
451
495
}
0 commit comments