Skip to content

Commit

Permalink
refactor: minor improvement (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Jun 17, 2024
1 parent 62b297e commit b965c63
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Options:
--light-theme Use light theme
--dry-run Display the message without sending it
--info Display information
--list-models List all available models
--list-models List all available chat models
--list-roles List all available roles
--list-sessions List all available sessions
-h, --help Print help
Expand Down
2 changes: 1 addition & 1 deletion scripts/completions/aichat.fish
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ complete -c aichat -s S -l no-stream -d 'Turns off stream mode'
complete -c aichat -l light-theme -d 'Use light theme'
complete -c aichat -l dry-run -d 'Display the message without sending it'
complete -c aichat -l info -d 'Display information'
complete -c aichat -l list-models -d 'List all available models'
complete -c aichat -l list-models -d 'List all available chat models'
complete -c aichat -l list-roles -d 'List all roles'
complete -c aichat -l list-sessions -d 'List all sessions'
complete -c aichat -l list-bots -d 'List all bots'
Expand Down
2 changes: 1 addition & 1 deletion scripts/completions/aichat.nu
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module completions {
--light-theme # Use light theme
--dry-run # Display the message without sending it
--info # Display information
--list-models # List all available models
--list-models # List all available chat models
--list-roles # List all roles
--list-sessions # List all sessions
--list-bots # List all bots
Expand Down
2 changes: 1 addition & 1 deletion scripts/completions/aichat.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Register-ArgumentCompleter -Native -CommandName 'aichat' -ScriptBlock {
[CompletionResult]::new('--light-theme', '--light-theme', [CompletionResultType]::ParameterName, 'Use light theme')
[CompletionResult]::new('--dry-run', '--dry-run', [CompletionResultType]::ParameterName, 'Display the message without sending it')
[CompletionResult]::new('--info', '--info', [CompletionResultType]::ParameterName, 'Display information')
[CompletionResult]::new('--list-models', '--list-models', [CompletionResultType]::ParameterName, 'List all available models')
[CompletionResult]::new('--list-models', '--list-models', [CompletionResultType]::ParameterName, 'List all available chat models')
[CompletionResult]::new('--list-roles', '--list-roles', [CompletionResultType]::ParameterName, 'List all roles')
[CompletionResult]::new('--list-sessions', '--list-sessions', [CompletionResultType]::ParameterName, 'List all sessions')
[CompletionResult]::new('--list-bots', '--list-bots', [CompletionResultType]::ParameterName, 'List all bots')
Expand Down
2 changes: 1 addition & 1 deletion scripts/completions/aichat.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ _aichat() {
'--light-theme[Use light theme]' \
'--dry-run[Display the message without sending it]' \
'--info[Display information]' \
'--list-models[List all available models]' \
'--list-models[List all available chat models]' \
'--list-roles[List all roles]' \
'--list-sessions[List all sessions]' \
'--list-bots[List all bots]' \
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct Cli {
/// Display information
#[clap(long)]
pub info: bool,
/// List all available models
/// List all available chat models
#[clap(long)]
pub list_models: bool,
/// List all roles
Expand Down
16 changes: 11 additions & 5 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ impl Config {
}

pub fn use_prompt(&mut self, prompt: &str) -> Result<()> {
let role = Role::new(TEMP_ROLE_NAME, prompt);
let mut role = Role::new(TEMP_ROLE_NAME, prompt);
role.set_model(&self.model);
self.use_role_obj(role)
}

Expand Down Expand Up @@ -705,9 +706,8 @@ impl Config {

pub fn exit_session(&mut self) -> Result<()> {
if let Some(mut session) = self.session.take() {
let is_repl = self.working_mode == WorkingMode::Repl;
let sessions_dir = self.sessions_dir()?;
session.exit(&sessions_dir, is_repl)?;
session.exit(&sessions_dir, self.working_mode.is_repl())?;
self.last_message = None;
}
Ok(())
Expand All @@ -723,7 +723,7 @@ impl Config {
};
let session_path = self.session_file(&name)?;
if let Some(session) = self.session.as_mut() {
session.save(&session_path)?;
session.save(&session_path, self.working_mode.is_repl())?;
}
Ok(())
}
Expand Down Expand Up @@ -933,8 +933,8 @@ impl Config {
}

pub fn exit_bot(&mut self) -> Result<()> {
self.exit_session()?;
if self.bot.take().is_some() {
self.exit_session()?;
self.rag.take();
self.last_message = None;
}
Expand Down Expand Up @@ -1420,6 +1420,12 @@ pub enum WorkingMode {
Serve,
}

impl WorkingMode {
pub fn is_repl(&self) -> bool {
*self == WorkingMode::Repl
}
}

bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StateFlags: u32 {
Expand Down
8 changes: 5 additions & 3 deletions src/config/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,12 @@ impl Session {
}
}
let session_path = session_dir.join(format!("{}.yaml", self.name()));
self.save(&session_path)?;
self.save(&session_path, is_repl)?;
}
Ok(())
}

pub fn save(&mut self, session_path: &Path) -> Result<()> {
pub fn save(&mut self, session_path: &Path, is_repl: bool) -> Result<()> {
if let Some(sessions_dir) = session_path.parent() {
if !sessions_dir.exists() {
create_dir_all(sessions_dir).with_context(|| {
Expand All @@ -338,7 +338,9 @@ impl Session {
)
})?;

println!("✨ Saved session to '{}'", session_path.display());
if is_repl {
println!("✨ Saved session to '{}'", session_path.display());
}

self.dirty = false;

Expand Down
2 changes: 1 addition & 1 deletion src/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ Tips: use <tab> to autocomplete conversation starter text.
".copy" => {
let config = self.config.read();
self.copy(config.last_reply())
.with_context(|| "Failed to copy the last output")?;
.with_context(|| "Failed to copy the last response")?;
}
".exit" => match args {
Some("role") => {
Expand Down

0 comments on commit b965c63

Please sign in to comment.