diff options
| author | sigoden <sigoden@gmail.com> | 2024-09-10 18:35:34 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-10 18:35:34 +0800 |
| commit | e181ae9b0d4814a7c005c94837426126dde63c6c (patch) | |
| tree | ed727036d586f0673198a4b30217bbe2e34a094b /src/config/mod.rs | |
| parent | 84e9515509c559ed01e4b0a67539f10cd2c065e6 (diff) | |
| download | aichat-e181ae9b0d4814a7c005c94837426126dde63c6c.tar.gz | |
refactor: extract built-in roles to embedded files (#853)
Diffstat (limited to 'src/config/mod.rs')
| -rw-r--r-- | src/config/mod.rs | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs index 5cd266f..dae2018 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -5,7 +5,7 @@ mod session; pub use self::agent::{list_agents, Agent}; pub use self::input::Input; -pub use self::role::{Role, RoleLike, BUILTIN_ROLES, CODE_ROLE, EXPLAIN_SHELL_ROLE, SHELL_ROLE}; +pub use self::role::{Role, RoleLike, CODE_ROLE, EXPLAIN_SHELL_ROLE, SHELL_ROLE}; use self::session::Session; use crate::client::{ @@ -774,11 +774,7 @@ impl Config { let content = read_to_string(&path)?; Role::new(name, &content) } else { - BUILTIN_ROLES - .iter() - .find(|v| v.name() == name) - .cloned() - .ok_or_else(|| anyhow!("Unknown role `{name}`"))? + Role::builtin(name)? }; match role.model_id() { Some(model_id) => { @@ -839,8 +835,11 @@ impl Config { if role_name == TEMP_ROLE_NAME { role_name = Text::new("Role name:") .with_validator(|input: &str| { - if input.trim().is_empty() { - Ok(Validation::Invalid("This field is required".into())) + let input = input.trim(); + if input.is_empty() { + Ok(Validation::Invalid("This name is required".into())) + } else if input == TEMP_ROLE_NAME { + Ok(Validation::Invalid("This name is reserved".into())) } else { Ok(Validation::Valid) } @@ -862,7 +861,7 @@ impl Config { } pub fn all_roles() -> Vec<Role> { - let mut roles: HashMap<String, Role> = BUILTIN_ROLES + let mut roles: HashMap<String, Role> = Role::list_builtin_roles() .iter() .map(|v| (v.name().to_string(), v.clone())) .collect(); @@ -894,7 +893,7 @@ impl Config { } } if with_builtin { - names.extend(BUILTIN_ROLES.iter().map(|v| v.name().to_string())); + names.extend(Role::list_builtin_role_names()); } let mut names: Vec<_> = names.into_iter().collect(); names.sort_unstable(); |
