2023-03-09 11:10:02 +00:00
|
|
|
mod role;
|
2023-10-28 13:39:17 +00:00
|
|
|
mod session;
|
2023-03-09 02:39:28 +00:00
|
|
|
|
2023-03-09 11:10:02 +00:00
|
|
|
use self::role::Role;
|
2023-10-28 13:39:17 +00:00
|
|
|
use self::session::{Session, TEMP_SESSION_NAME};
|
2023-03-09 02:39:28 +00:00
|
|
|
|
2023-10-31 08:50:08 +00:00
|
|
|
use crate::client::{
|
2023-11-02 07:33:22 +00:00
|
|
|
create_client_config, list_client_types, list_models, ClientConfig, ExtraConfig, Message,
|
2023-11-02 02:45:11 +00:00
|
|
|
ModelInfo, OpenAIClient, SendData,
|
2023-10-31 08:50:08 +00:00
|
|
|
};
|
2023-11-02 01:53:54 +00:00
|
|
|
use crate::render::{MarkdownRender, RenderOptions};
|
2023-11-01 02:28:54 +00:00
|
|
|
use crate::utils::{get_env_name, light_theme_from_colorfgbg, now, prompt_op_err};
|
2023-03-09 00:11:38 +00:00
|
|
|
|
2023-03-09 02:39:28 +00:00
|
|
|
use anyhow::{anyhow, bail, Context, Result};
|
2023-10-28 13:39:17 +00:00
|
|
|
use inquire::{Confirm, Select, Text};
|
2023-10-30 09:28:10 +00:00
|
|
|
use is_terminal::IsTerminal;
|
2023-03-11 13:45:34 +00:00
|
|
|
use parking_lot::RwLock;
|
2023-03-09 11:10:02 +00:00
|
|
|
use serde::Deserialize;
|
2023-03-03 01:15:58 +00:00
|
|
|
use std::{
|
2023-03-03 01:38:53 +00:00
|
|
|
env,
|
2023-10-28 13:39:17 +00:00
|
|
|
fs::{create_dir_all, read_dir, read_to_string, remove_file, File, OpenOptions},
|
2023-10-30 09:28:10 +00:00
|
|
|
io::{stdout, Write},
|
2023-03-03 01:15:58 +00:00
|
|
|
path::{Path, PathBuf},
|
2023-03-03 09:53:29 +00:00
|
|
|
process::exit,
|
2023-03-05 14:51:29 +00:00
|
|
|
sync::Arc,
|
2023-03-03 01:15:58 +00:00
|
|
|
};
|
2023-10-31 01:06:41 +00:00
|
|
|
use syntect::highlighting::ThemeSet;
|
|
|
|
|
|
|
|
/// Monokai Extended
|
|
|
|
const DARK_THEME: &[u8] = include_bytes!("../../assets/monokai-extended.theme.bin");
|
|
|
|
const LIGHT_THEME: &[u8] = include_bytes!("../../assets/monokai-extended-light.theme.bin");
|
2023-03-02 11:52:11 +00:00
|
|
|
|
2023-03-03 04:43:34 +00:00
|
|
|
const CONFIG_FILE_NAME: &str = "config.yaml";
|
|
|
|
const ROLES_FILE_NAME: &str = "roles.yaml";
|
2023-10-28 13:39:17 +00:00
|
|
|
const MESSAGES_FILE_NAME: &str = "messages.md";
|
|
|
|
const SESSIONS_DIR_NAME: &str = "sessions";
|
|
|
|
|
2023-11-01 02:28:54 +00:00
|
|
|
const CLIENTS_FIELD: &str = "clients";
|
|
|
|
|
2023-03-02 11:52:11 +00:00
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
2023-03-11 03:26:24 +00:00
|
|
|
#[serde(default)]
|
2023-03-02 11:52:11 +00:00
|
|
|
pub struct Config {
|
2023-10-26 08:42:54 +00:00
|
|
|
/// LLM model
|
|
|
|
pub model: Option<String>,
|
2023-10-31 09:34:16 +00:00
|
|
|
/// GPT temperature, between 0 and 2
|
2023-10-30 04:58:11 +00:00
|
|
|
#[serde(rename(serialize = "temperature", deserialize = "temperature"))]
|
|
|
|
pub default_temperature: Option<f64>,
|
2023-10-31 09:34:16 +00:00
|
|
|
/// Whether to save the message
|
2023-03-03 01:38:53 +00:00
|
|
|
pub save: bool,
|
2023-03-04 01:22:55 +00:00
|
|
|
/// Whether to disable highlight
|
2023-03-05 11:48:22 +00:00
|
|
|
pub highlight: bool,
|
2023-11-01 02:28:54 +00:00
|
|
|
/// Dry-run flag
|
2023-03-02 11:52:11 +00:00
|
|
|
pub dry_run: bool,
|
2023-10-31 09:34:16 +00:00
|
|
|
/// Whether to use a light theme
|
2023-03-11 10:53:36 +00:00
|
|
|
pub light_theme: bool,
|
2023-10-31 10:34:38 +00:00
|
|
|
/// Specify the text-wrapping mode (no, auto, <max-width>)
|
2023-10-30 02:07:01 +00:00
|
|
|
pub wrap: Option<String>,
|
2023-10-30 07:08:49 +00:00
|
|
|
/// Whether wrap code block
|
2023-10-30 02:07:01 +00:00
|
|
|
pub wrap_code: bool,
|
2023-05-04 14:42:50 +00:00
|
|
|
/// Automatically copy the last output to the clipboard
|
2023-05-04 13:22:12 +00:00
|
|
|
pub auto_copy: bool,
|
2023-10-31 09:34:16 +00:00
|
|
|
/// REPL keybindings. values: emacs, vi
|
2023-10-27 13:22:24 +00:00
|
|
|
pub keybindings: Keybindings,
|
2023-10-28 14:22:37 +00:00
|
|
|
/// Setup AIs
|
2023-10-26 08:42:54 +00:00
|
|
|
pub clients: Vec<ClientConfig>,
|
2023-03-02 14:25:55 +00:00
|
|
|
/// Predefined roles
|
2023-03-09 07:30:39 +00:00
|
|
|
#[serde(skip)]
|
2023-03-02 11:52:11 +00:00
|
|
|
pub roles: Vec<Role>,
|
2023-03-05 14:51:29 +00:00
|
|
|
/// Current selected role
|
2023-03-09 07:30:39 +00:00
|
|
|
#[serde(skip)]
|
2023-03-05 14:51:29 +00:00
|
|
|
pub role: Option<Role>,
|
2023-10-28 13:39:17 +00:00
|
|
|
/// Current session
|
2023-03-09 07:30:39 +00:00
|
|
|
#[serde(skip)]
|
2023-10-28 13:39:17 +00:00
|
|
|
pub session: Option<Session>,
|
2023-03-16 09:02:09 +00:00
|
|
|
#[serde(skip)]
|
2023-10-26 08:42:54 +00:00
|
|
|
pub model_info: ModelInfo,
|
2023-10-28 13:39:17 +00:00
|
|
|
#[serde(skip)]
|
|
|
|
pub last_message: Option<(String, String)>,
|
2023-10-30 04:58:11 +00:00
|
|
|
#[serde(skip)]
|
|
|
|
pub temperature: Option<f64>,
|
2023-03-02 11:52:11 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 03:26:24 +00:00
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2023-10-26 08:42:54 +00:00
|
|
|
model: None,
|
2023-10-30 04:58:11 +00:00
|
|
|
default_temperature: None,
|
2023-10-31 08:50:08 +00:00
|
|
|
save: true,
|
2023-03-11 03:26:24 +00:00
|
|
|
highlight: true,
|
|
|
|
dry_run: false,
|
2023-03-11 10:53:36 +00:00
|
|
|
light_theme: false,
|
2023-10-30 02:07:01 +00:00
|
|
|
wrap: None,
|
|
|
|
wrap_code: false,
|
2023-05-04 13:22:12 +00:00
|
|
|
auto_copy: false,
|
2023-10-27 13:22:24 +00:00
|
|
|
keybindings: Default::default(),
|
2023-11-01 02:28:54 +00:00
|
|
|
clients: vec![ClientConfig::default()],
|
2023-10-28 13:39:17 +00:00
|
|
|
roles: vec![],
|
2023-03-11 03:26:24 +00:00
|
|
|
role: None,
|
2023-10-28 13:39:17 +00:00
|
|
|
session: None,
|
2023-10-26 08:42:54 +00:00
|
|
|
model_info: Default::default(),
|
2023-10-28 13:39:17 +00:00
|
|
|
last_message: None,
|
2023-10-30 04:58:11 +00:00
|
|
|
temperature: None,
|
2023-03-11 03:26:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 02:45:11 +00:00
|
|
|
pub type GlobalConfig = Arc<RwLock<Config>>;
|
2023-03-05 14:51:29 +00:00
|
|
|
|
2023-03-02 11:52:11 +00:00
|
|
|
impl Config {
|
2023-03-11 03:26:24 +00:00
|
|
|
pub fn init(is_interactive: bool) -> Result<Self> {
|
|
|
|
let config_path = Self::config_file()?;
|
2023-10-26 08:42:54 +00:00
|
|
|
|
2023-10-28 14:10:17 +00:00
|
|
|
let api_key = env::var("OPENAI_API_KEY").ok();
|
2023-10-26 08:42:54 +00:00
|
|
|
|
|
|
|
let exist_config_path = config_path.exists();
|
|
|
|
if is_interactive && api_key.is_none() && !exist_config_path {
|
2023-03-03 09:53:29 +00:00
|
|
|
create_config_file(&config_path)?;
|
|
|
|
}
|
2023-10-26 08:42:54 +00:00
|
|
|
let mut config = if api_key.is_some() && !exist_config_path {
|
2023-07-10 03:07:09 +00:00
|
|
|
Self::default()
|
2023-03-11 03:26:24 +00:00
|
|
|
} else {
|
|
|
|
Self::load_config(&config_path)?
|
|
|
|
};
|
2023-10-26 08:42:54 +00:00
|
|
|
|
|
|
|
// Compatible with old configuration files
|
|
|
|
if exist_config_path {
|
|
|
|
config.compat_old_config(&config_path)?;
|
2023-03-11 03:26:24 +00:00
|
|
|
}
|
2023-10-26 08:42:54 +00:00
|
|
|
|
2023-10-30 02:07:01 +00:00
|
|
|
if let Some(wrap) = config.wrap.clone() {
|
|
|
|
config.set_wrap(&wrap)?;
|
|
|
|
}
|
2023-10-28 13:39:17 +00:00
|
|
|
|
2023-10-30 04:58:11 +00:00
|
|
|
config.temperature = config.default_temperature;
|
|
|
|
|
2023-03-03 01:38:53 +00:00
|
|
|
config.load_roles()?;
|
2023-11-01 02:28:54 +00:00
|
|
|
|
|
|
|
config.setup_model_info()?;
|
|
|
|
config.setup_highlight();
|
|
|
|
config.setup_light_theme()?;
|
2023-03-09 15:30:12 +00:00
|
|
|
|
2023-03-02 11:52:11 +00:00
|
|
|
Ok(config)
|
|
|
|
}
|
2023-03-03 04:43:34 +00:00
|
|
|
|
2023-10-30 11:26:06 +00:00
|
|
|
pub fn retrieve_role(&self, name: &str) -> Result<Role> {
|
|
|
|
self.roles
|
|
|
|
.iter()
|
|
|
|
.find(|v| v.match_name(name))
|
|
|
|
.map(|v| {
|
|
|
|
let mut role = v.clone();
|
|
|
|
role.complete_prompt_args(name);
|
|
|
|
role
|
|
|
|
})
|
|
|
|
.ok_or_else(|| anyhow!("Unknown role `{name}`"))
|
2023-03-03 09:53:29 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 21:50:30 +00:00
|
|
|
pub fn config_dir() -> Result<PathBuf> {
|
2023-03-11 03:26:24 +00:00
|
|
|
let env_name = get_env_name("config_dir");
|
2023-07-10 03:07:09 +00:00
|
|
|
let path = if let Some(v) = env::var_os(env_name) {
|
|
|
|
PathBuf::from(v)
|
|
|
|
} else {
|
|
|
|
let mut dir = dirs::config_dir().ok_or_else(|| anyhow!("Not found config dir"))?;
|
|
|
|
dir.push(env!("CARGO_CRATE_NAME"));
|
|
|
|
dir
|
2023-03-03 01:38:53 +00:00
|
|
|
};
|
2023-03-04 21:50:30 +00:00
|
|
|
Ok(path)
|
|
|
|
}
|
|
|
|
|
2023-10-28 13:39:17 +00:00
|
|
|
pub fn local_path(name: &str) -> Result<PathBuf> {
|
2023-03-04 21:50:30 +00:00
|
|
|
let mut path = Self::config_dir()?;
|
2023-03-03 01:38:53 +00:00
|
|
|
path.push(name);
|
|
|
|
Ok(path)
|
|
|
|
}
|
2023-03-03 04:43:34 +00:00
|
|
|
|
2023-10-28 13:39:17 +00:00
|
|
|
pub fn save_message(&mut self, input: &str, output: &str) -> Result<()> {
|
|
|
|
self.last_message = Some((input.to_string(), output.to_string()));
|
|
|
|
|
2023-10-30 02:07:01 +00:00
|
|
|
if self.dry_run {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2023-10-28 13:39:17 +00:00
|
|
|
if let Some(session) = self.session.as_mut() {
|
|
|
|
session.add_message(input, output)?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2023-03-03 09:53:29 +00:00
|
|
|
if !self.save {
|
2023-03-08 03:27:51 +00:00
|
|
|
return Ok(());
|
2023-03-03 09:53:29 +00:00
|
|
|
}
|
2023-03-08 03:27:51 +00:00
|
|
|
let mut file = self.open_message_file()?;
|
|
|
|
if output.is_empty() || !self.save {
|
2023-03-05 22:45:00 +00:00
|
|
|
return Ok(());
|
2023-03-05 14:51:29 +00:00
|
|
|
}
|
2023-03-05 22:45:00 +00:00
|
|
|
let timestamp = now();
|
|
|
|
let output = match self.role.as_ref() {
|
|
|
|
None => {
|
2023-03-08 05:44:37 +00:00
|
|
|
format!("# CHAT:[{timestamp}]\n{input}\n--------\n{output}\n--------\n\n",)
|
2023-03-05 22:45:00 +00:00
|
|
|
}
|
|
|
|
Some(v) => {
|
2023-07-10 03:07:09 +00:00
|
|
|
format!(
|
|
|
|
"# CHAT:[{timestamp}] ({})\n{input}\n--------\n{output}\n--------\n\n",
|
|
|
|
v.name,
|
|
|
|
)
|
2023-03-05 22:45:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
file.write_all(output.as_bytes())
|
|
|
|
.with_context(|| "Failed to save message")
|
2023-03-03 09:53:29 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 04:43:34 +00:00
|
|
|
pub fn config_file() -> Result<PathBuf> {
|
2023-10-28 13:39:17 +00:00
|
|
|
Self::local_path(CONFIG_FILE_NAME)
|
2023-03-03 04:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn roles_file() -> Result<PathBuf> {
|
2023-03-11 03:26:24 +00:00
|
|
|
let env_name = get_env_name("roles_file");
|
2023-07-10 03:07:09 +00:00
|
|
|
env::var(env_name).map_or_else(
|
2023-10-28 13:39:17 +00:00
|
|
|
|_| Self::local_path(ROLES_FILE_NAME),
|
2023-07-10 03:07:09 +00:00
|
|
|
|value| Ok(PathBuf::from(value)),
|
|
|
|
)
|
2023-03-03 04:43:34 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 21:50:30 +00:00
|
|
|
pub fn messages_file() -> Result<PathBuf> {
|
2023-10-28 13:39:17 +00:00
|
|
|
Self::local_path(MESSAGES_FILE_NAME)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sessions_dir() -> Result<PathBuf> {
|
|
|
|
Self::local_path(SESSIONS_DIR_NAME)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn session_file(name: &str) -> Result<PathBuf> {
|
|
|
|
let mut path = Self::sessions_dir()?;
|
|
|
|
path.push(&format!("{name}.yaml"));
|
|
|
|
Ok(path)
|
2023-03-03 04:43:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 11:26:06 +00:00
|
|
|
pub fn set_role(&mut self, name: &str) -> Result<()> {
|
|
|
|
let role = self.retrieve_role(name)?;
|
|
|
|
if let Some(session) = self.session.as_mut() {
|
|
|
|
session.update_role(Some(role.clone()))?;
|
2023-03-05 14:51:29 +00:00
|
|
|
}
|
2023-10-30 11:26:06 +00:00
|
|
|
self.temperature = role.temperature;
|
|
|
|
self.role = Some(role);
|
|
|
|
Ok(())
|
2023-03-05 14:51:29 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 23:13:22 +00:00
|
|
|
pub fn clear_role(&mut self) -> Result<()> {
|
2023-10-28 13:39:17 +00:00
|
|
|
if let Some(session) = self.session.as_mut() {
|
|
|
|
session.update_role(None)?;
|
2023-03-09 23:13:22 +00:00
|
|
|
}
|
2023-10-30 04:58:11 +00:00
|
|
|
self.temperature = self.default_temperature;
|
2023-03-09 23:13:22 +00:00
|
|
|
self.role = None;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-08 11:43:11 +00:00
|
|
|
pub fn get_temperature(&self) -> Option<f64> {
|
2023-10-30 04:58:11 +00:00
|
|
|
self.temperature
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_temperature(&mut self, value: Option<f64>) -> Result<()> {
|
|
|
|
self.temperature = value;
|
|
|
|
if let Some(session) = self.session.as_mut() {
|
2023-11-01 14:15:55 +00:00
|
|
|
session.set_temperature(value);
|
2023-10-30 04:58:11 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
2023-03-08 11:43:11 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 02:39:28 +00:00
|
|
|
pub fn echo_messages(&self, content: &str) -> String {
|
2023-10-28 13:39:17 +00:00
|
|
|
if let Some(session) = self.session.as_ref() {
|
|
|
|
session.echo_messages(content)
|
2023-03-09 02:39:28 +00:00
|
|
|
} else if let Some(role) = self.role.as_ref() {
|
2023-03-09 11:10:02 +00:00
|
|
|
role.echo_messages(content)
|
2023-03-09 02:39:28 +00:00
|
|
|
} else {
|
|
|
|
content.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 11:10:02 +00:00
|
|
|
pub fn build_messages(&self, content: &str) -> Result<Vec<Message>> {
|
2023-10-28 13:39:17 +00:00
|
|
|
let messages = if let Some(session) = self.session.as_ref() {
|
|
|
|
session.build_emssages(content)
|
2023-03-09 02:39:28 +00:00
|
|
|
} else if let Some(role) = self.role.as_ref() {
|
2023-07-10 03:07:09 +00:00
|
|
|
role.build_messages(content)
|
2023-03-09 02:39:28 +00:00
|
|
|
} else {
|
2023-03-09 11:10:02 +00:00
|
|
|
let message = Message::new(content);
|
|
|
|
vec![message]
|
2023-03-09 07:30:39 +00:00
|
|
|
};
|
2023-03-09 11:10:02 +00:00
|
|
|
Ok(messages)
|
2023-03-08 09:13:11 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 02:07:01 +00:00
|
|
|
pub fn set_wrap(&mut self, value: &str) -> Result<()> {
|
|
|
|
if value == "no" {
|
|
|
|
self.wrap = None;
|
|
|
|
} else if value == "auto" {
|
|
|
|
self.wrap = Some(value.into());
|
|
|
|
} else {
|
|
|
|
value
|
|
|
|
.parse::<u16>()
|
|
|
|
.map_err(|_| anyhow!("Invalid wrap value"))?;
|
|
|
|
self.wrap = Some(value.into())
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-26 08:42:54 +00:00
|
|
|
pub fn set_model(&mut self, value: &str) -> Result<()> {
|
2023-11-02 07:33:22 +00:00
|
|
|
let models = list_models(self);
|
2023-10-28 13:39:17 +00:00
|
|
|
let mut model_info = None;
|
2023-11-02 07:33:22 +00:00
|
|
|
let value = value.trim_end_matches(':');
|
2023-10-26 08:42:54 +00:00
|
|
|
if value.contains(':') {
|
2023-11-02 07:33:22 +00:00
|
|
|
if let Some(model) = models.iter().find(|v| v.id() == value) {
|
2023-10-28 13:39:17 +00:00
|
|
|
model_info = Some(model.clone());
|
2023-10-26 08:42:54 +00:00
|
|
|
}
|
|
|
|
} else if let Some(model) = models.iter().find(|v| v.client == value) {
|
2023-10-28 13:39:17 +00:00
|
|
|
model_info = Some(model.clone());
|
|
|
|
}
|
|
|
|
match model_info {
|
2023-10-31 08:50:08 +00:00
|
|
|
None => bail!("Unknown model '{}'", value),
|
2023-10-28 13:39:17 +00:00
|
|
|
Some(model_info) => {
|
|
|
|
if let Some(session) = self.session.as_mut() {
|
2023-11-01 14:15:55 +00:00
|
|
|
session.set_model(model_info.clone())?;
|
2023-10-28 13:39:17 +00:00
|
|
|
}
|
|
|
|
self.model_info = model_info;
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-03-16 09:02:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 01:53:54 +00:00
|
|
|
pub fn sys_info(&self) -> Result<String> {
|
2023-10-28 13:39:17 +00:00
|
|
|
let path_info = |path: &Path| {
|
2023-03-05 14:51:29 +00:00
|
|
|
let state = if path.exists() { "" } else { " ⚠️" };
|
|
|
|
format!("{}{state}", path.display())
|
|
|
|
};
|
|
|
|
let temperature = self
|
|
|
|
.temperature
|
2023-07-10 03:07:09 +00:00
|
|
|
.map_or_else(|| String::from("-"), |v| v.to_string());
|
2023-10-30 02:07:01 +00:00
|
|
|
let wrap = self
|
|
|
|
.wrap
|
|
|
|
.clone()
|
|
|
|
.map_or_else(|| String::from("no"), |v| v.to_string());
|
2023-03-05 14:51:29 +00:00
|
|
|
let items = vec![
|
2023-11-02 07:33:22 +00:00
|
|
|
("model", self.model_info.id()),
|
2023-03-05 14:51:29 +00:00
|
|
|
("temperature", temperature),
|
2023-11-02 01:53:54 +00:00
|
|
|
("dry_run", self.dry_run.to_string()),
|
2023-03-05 14:51:29 +00:00
|
|
|
("save", self.save.to_string()),
|
|
|
|
("highlight", self.highlight.to_string()),
|
2023-10-30 02:07:01 +00:00
|
|
|
("wrap", wrap),
|
|
|
|
("wrap_code", self.wrap_code.to_string()),
|
2023-11-02 01:53:54 +00:00
|
|
|
("light_theme", self.light_theme.to_string()),
|
2023-10-27 13:22:24 +00:00
|
|
|
("keybindings", self.keybindings.stringify().into()),
|
2023-11-02 01:53:54 +00:00
|
|
|
("config_file", path_info(&Self::config_file()?)),
|
|
|
|
("roles_file", path_info(&Self::roles_file()?)),
|
|
|
|
("messages_file", path_info(&Self::messages_file()?)),
|
|
|
|
("sessions_dir", path_info(&Self::sessions_dir()?)),
|
2023-03-05 14:51:29 +00:00
|
|
|
];
|
2023-11-02 01:53:54 +00:00
|
|
|
let output = items
|
|
|
|
.iter()
|
|
|
|
.map(|(name, value)| format!("{name:<20}{value}"))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("\n");
|
2023-03-05 14:51:29 +00:00
|
|
|
Ok(output)
|
|
|
|
}
|
|
|
|
|
2023-11-02 01:53:54 +00:00
|
|
|
pub fn role_info(&self) -> Result<String> {
|
|
|
|
if let Some(role) = &self.role {
|
|
|
|
role.info()
|
|
|
|
} else {
|
|
|
|
bail!("No role")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn session_info(&self) -> Result<String> {
|
|
|
|
if let Some(session) = &self.session {
|
|
|
|
let render_options = self.get_render_options()?;
|
|
|
|
let mut markdown_render = MarkdownRender::init(render_options)?;
|
|
|
|
session.render(&mut markdown_render)
|
|
|
|
} else {
|
|
|
|
bail!("No session")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn info(&self) -> Result<String> {
|
|
|
|
if let Some(session) = &self.session {
|
|
|
|
session.export()
|
|
|
|
} else if let Some(role) = &self.role {
|
|
|
|
role.info()
|
|
|
|
} else {
|
|
|
|
self.sys_info()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_reply(&self) -> &str {
|
|
|
|
self.last_message
|
|
|
|
.as_ref()
|
|
|
|
.map(|(_, reply)| reply.as_str())
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2023-11-02 07:33:22 +00:00
|
|
|
pub fn repl_complete(&self, cmd: &str, args: &str) -> Vec<String> {
|
|
|
|
let possible_values = match cmd {
|
|
|
|
".role" => self.roles.iter().map(|v| v.name.clone()).collect(),
|
|
|
|
".model" => list_models(self).into_iter().map(|v| v.id()).collect(),
|
|
|
|
".session" => self.list_sessions(),
|
|
|
|
".set" => {
|
|
|
|
vec![
|
|
|
|
"temperature ".into(),
|
|
|
|
format!("save {}", !self.save),
|
|
|
|
format!("highlight {}", !self.highlight),
|
|
|
|
format!("dry_run {}", !self.dry_run),
|
2023-11-02 08:08:11 +00:00
|
|
|
format!("auto_copy {}", !self.auto_copy),
|
2023-11-02 07:33:22 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
_ => vec![],
|
|
|
|
};
|
|
|
|
let mut possible_values: Vec<String> = possible_values
|
|
|
|
.into_iter()
|
|
|
|
.filter(|v| v.starts_with(args))
|
2023-03-05 22:45:00 +00:00
|
|
|
.collect();
|
2023-11-02 07:33:22 +00:00
|
|
|
possible_values.sort_unstable();
|
|
|
|
possible_values
|
2023-03-05 22:45:00 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 02:39:28 +00:00
|
|
|
pub fn update(&mut self, data: &str) -> Result<()> {
|
2023-03-05 14:51:29 +00:00
|
|
|
let parts: Vec<&str> = data.split_whitespace().collect();
|
|
|
|
if parts.len() != 2 {
|
2023-03-09 02:39:28 +00:00
|
|
|
bail!("Usage: .set <key> <value>. If value is null, unset key.");
|
2023-03-05 14:51:29 +00:00
|
|
|
}
|
|
|
|
let key = parts[0];
|
|
|
|
let value = parts[1];
|
|
|
|
let unset = value == "null";
|
|
|
|
match key {
|
|
|
|
"temperature" => {
|
2023-10-30 04:58:11 +00:00
|
|
|
let value = if unset {
|
|
|
|
None
|
2023-03-05 14:51:29 +00:00
|
|
|
} else {
|
|
|
|
let value = value.parse().with_context(|| "Invalid value")?;
|
2023-10-30 04:58:11 +00:00
|
|
|
Some(value)
|
|
|
|
};
|
|
|
|
self.set_temperature(value)?;
|
2023-03-05 14:51:29 +00:00
|
|
|
}
|
|
|
|
"save" => {
|
|
|
|
let value = value.parse().with_context(|| "Invalid value")?;
|
|
|
|
self.save = value;
|
|
|
|
}
|
|
|
|
"highlight" => {
|
|
|
|
let value = value.parse().with_context(|| "Invalid value")?;
|
|
|
|
self.highlight = value;
|
|
|
|
}
|
|
|
|
"dry_run" => {
|
|
|
|
let value = value.parse().with_context(|| "Invalid value")?;
|
|
|
|
self.dry_run = value;
|
|
|
|
}
|
2023-11-02 08:08:11 +00:00
|
|
|
"auto_copy" => {
|
|
|
|
let value = value.parse().with_context(|| "Invalid value")?;
|
|
|
|
self.auto_copy = value;
|
|
|
|
}
|
2023-10-30 04:58:11 +00:00
|
|
|
_ => bail!("Unknown key `{key}`"),
|
2023-03-05 14:51:29 +00:00
|
|
|
}
|
2023-03-09 02:39:28 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-11-02 01:53:54 +00:00
|
|
|
pub fn start_session(&mut self, session: Option<&str>) -> Result<()> {
|
2023-10-28 13:39:17 +00:00
|
|
|
if self.session.is_some() {
|
|
|
|
bail!("Already in a session, please use '.clear session' to exit the session first?");
|
|
|
|
}
|
|
|
|
match session {
|
|
|
|
None => {
|
|
|
|
let session_file = Self::session_file(TEMP_SESSION_NAME)?;
|
|
|
|
if session_file.exists() {
|
|
|
|
remove_file(session_file)
|
|
|
|
.with_context(|| "Failed to clean previous session")?;
|
|
|
|
}
|
|
|
|
self.session = Some(Session::new(
|
|
|
|
TEMP_SESSION_NAME,
|
2023-11-01 14:15:55 +00:00
|
|
|
self.model_info.clone(),
|
2023-10-28 13:39:17 +00:00
|
|
|
self.role.clone(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Some(name) => {
|
|
|
|
let session_path = Self::session_file(name)?;
|
|
|
|
if !session_path.exists() {
|
|
|
|
self.session = Some(Session::new(
|
|
|
|
name,
|
2023-11-01 14:15:55 +00:00
|
|
|
self.model_info.clone(),
|
2023-10-28 13:39:17 +00:00
|
|
|
self.role.clone(),
|
|
|
|
));
|
|
|
|
} else {
|
2023-10-30 04:58:11 +00:00
|
|
|
let session = Session::load(name, &session_path)?;
|
2023-11-01 14:15:55 +00:00
|
|
|
let model = session.model().to_string();
|
|
|
|
self.temperature = session.temperature();
|
2023-10-28 13:39:17 +00:00
|
|
|
self.session = Some(session);
|
2023-10-30 04:58:11 +00:00
|
|
|
self.set_model(&model)?;
|
2023-10-28 13:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(session) = self.session.as_mut() {
|
|
|
|
if session.is_empty() {
|
|
|
|
if let Some((input, output)) = &self.last_message {
|
|
|
|
let ans = Confirm::new(
|
|
|
|
"Start a session that incorporates the last question and answer?",
|
|
|
|
)
|
|
|
|
.with_default(false)
|
2023-11-01 14:15:55 +00:00
|
|
|
.prompt()
|
|
|
|
.map_err(prompt_op_err)?;
|
2023-10-28 13:39:17 +00:00
|
|
|
if ans {
|
|
|
|
session.add_message(input, output)?;
|
|
|
|
}
|
|
|
|
}
|
2023-03-09 02:39:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-28 13:39:17 +00:00
|
|
|
pub fn end_session(&mut self) -> Result<()> {
|
|
|
|
if let Some(mut session) = self.session.take() {
|
|
|
|
self.last_message = None;
|
2023-10-30 04:58:11 +00:00
|
|
|
self.temperature = self.default_temperature;
|
2023-10-28 13:39:17 +00:00
|
|
|
if session.should_save() {
|
2023-11-01 14:15:55 +00:00
|
|
|
let ans = Confirm::new("Save session?")
|
|
|
|
.with_default(false)
|
|
|
|
.prompt()
|
|
|
|
.map_err(prompt_op_err)?;
|
2023-10-28 13:39:17 +00:00
|
|
|
if !ans {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2023-11-01 14:15:55 +00:00
|
|
|
let mut name = session.name().to_string();
|
2023-10-28 13:39:17 +00:00
|
|
|
if session.is_temp() {
|
2023-11-01 14:15:55 +00:00
|
|
|
name = Text::new("Session name:")
|
|
|
|
.with_default(&name)
|
|
|
|
.prompt()
|
|
|
|
.map_err(prompt_op_err)?;
|
2023-10-28 13:39:17 +00:00
|
|
|
}
|
|
|
|
let session_path = Self::session_file(&name)?;
|
2023-11-01 02:28:54 +00:00
|
|
|
let sessions_dir = session_path.parent().ok_or_else(|| {
|
|
|
|
anyhow!("Unable to save session file to {}", session_path.display())
|
|
|
|
})?;
|
|
|
|
if !sessions_dir.exists() {
|
|
|
|
create_dir_all(sessions_dir).with_context(|| {
|
|
|
|
format!("Failed to create session_dir '{}'", sessions_dir.display())
|
|
|
|
})?;
|
|
|
|
}
|
2023-10-28 13:39:17 +00:00
|
|
|
session.save(&session_path)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
2023-03-09 02:39:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 07:33:22 +00:00
|
|
|
pub fn list_sessions(&self) -> Vec<String> {
|
|
|
|
let sessions_dir = match Self::sessions_dir() {
|
|
|
|
Ok(dir) => dir,
|
|
|
|
Err(_) => return vec![],
|
|
|
|
};
|
2023-10-28 13:39:17 +00:00
|
|
|
match read_dir(&sessions_dir) {
|
|
|
|
Ok(rd) => {
|
|
|
|
let mut names = vec![];
|
2023-11-02 07:33:22 +00:00
|
|
|
for entry in rd.flatten() {
|
2023-10-28 13:39:17 +00:00
|
|
|
let name = entry.file_name();
|
|
|
|
if let Some(name) = name.to_string_lossy().strip_suffix(".yaml") {
|
|
|
|
names.push(name.to_string());
|
|
|
|
}
|
|
|
|
}
|
2023-11-02 07:33:22 +00:00
|
|
|
names
|
2023-10-28 13:39:17 +00:00
|
|
|
}
|
2023-11-02 07:33:22 +00:00
|
|
|
Err(_) => vec![],
|
2023-03-09 02:39:28 +00:00
|
|
|
}
|
2023-03-05 14:51:29 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 01:06:41 +00:00
|
|
|
pub fn get_render_options(&self) -> Result<RenderOptions> {
|
|
|
|
let theme = if self.highlight {
|
|
|
|
let theme_mode = if self.light_theme { "light" } else { "dark" };
|
|
|
|
let theme_filename = format!("{theme_mode}.tmTheme");
|
|
|
|
let theme_path = Self::local_path(&theme_filename)?;
|
|
|
|
if theme_path.exists() {
|
|
|
|
let theme = ThemeSet::get_theme(&theme_path)
|
|
|
|
.with_context(|| format!("Invalid theme at {}", theme_path.display()))?;
|
|
|
|
Some(theme)
|
|
|
|
} else {
|
|
|
|
let theme = if self.light_theme {
|
|
|
|
bincode::deserialize_from(LIGHT_THEME).expect("Invalid builtin light theme")
|
|
|
|
} else {
|
|
|
|
bincode::deserialize_from(DARK_THEME).expect("Invalid builtin dark theme")
|
|
|
|
};
|
|
|
|
Some(theme)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2023-10-30 09:28:10 +00:00
|
|
|
let wrap = if stdout().is_terminal() {
|
|
|
|
self.wrap.clone()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2023-10-31 01:06:41 +00:00
|
|
|
Ok(RenderOptions::new(theme, wrap, self.wrap_code))
|
2023-03-11 13:45:34 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 02:28:54 +00:00
|
|
|
pub fn render_prompt_right(&self) -> String {
|
|
|
|
if let Some(session) = &self.session {
|
2023-11-01 14:15:55 +00:00
|
|
|
let (tokens, percent) = session.tokens_and_percent();
|
|
|
|
let percent = if percent == 0.0 {
|
|
|
|
String::new()
|
|
|
|
} else {
|
|
|
|
format!("({percent}%)")
|
|
|
|
};
|
|
|
|
format!("{tokens}{percent}")
|
2023-11-01 02:28:54 +00:00
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 08:50:08 +00:00
|
|
|
pub fn prepare_send_data(&self, content: &str, stream: bool) -> Result<SendData> {
|
|
|
|
let messages = self.build_messages(content)?;
|
2023-11-01 14:15:55 +00:00
|
|
|
self.model_info.max_tokens_limit(&messages)?;
|
2023-10-31 08:50:08 +00:00
|
|
|
Ok(SendData {
|
|
|
|
messages,
|
|
|
|
temperature: self.get_temperature(),
|
|
|
|
stream,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-20 14:51:51 +00:00
|
|
|
pub fn maybe_print_send_tokens(&self, input: &str) {
|
|
|
|
if self.dry_run {
|
|
|
|
if let Ok(messages) = self.build_messages(input) {
|
2023-11-01 14:48:49 +00:00
|
|
|
let tokens = self.model_info.total_tokens(&messages);
|
2023-10-27 01:33:34 +00:00
|
|
|
println!(">>> This message consumes {tokens} tokens. <<<");
|
2023-03-20 14:51:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 03:27:51 +00:00
|
|
|
fn open_message_file(&self) -> Result<File> {
|
2023-07-10 03:07:09 +00:00
|
|
|
let path = Self::messages_file()?;
|
2023-03-11 03:26:24 +00:00
|
|
|
ensure_parent_exists(&path)?;
|
2023-03-08 03:27:51 +00:00
|
|
|
OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.append(true)
|
|
|
|
.open(&path)
|
|
|
|
.with_context(|| format!("Failed to create/append {}", path.display()))
|
|
|
|
}
|
|
|
|
|
2023-03-11 03:26:24 +00:00
|
|
|
fn load_config(config_path: &Path) -> Result<Self> {
|
2023-11-01 02:28:54 +00:00
|
|
|
let ctx = || format!("Failed to load config at {}", config_path.display());
|
|
|
|
let content = read_to_string(config_path).with_context(ctx)?;
|
2023-03-11 03:26:24 +00:00
|
|
|
|
2023-07-10 03:07:09 +00:00
|
|
|
let config: Self = serde_yaml::from_str(&content)
|
2023-11-01 02:28:54 +00:00
|
|
|
.map_err(|err| {
|
|
|
|
let err_msg = err.to_string();
|
|
|
|
if err_msg.starts_with(&format!("{}: ", CLIENTS_FIELD)) {
|
|
|
|
anyhow!("clients: invalid value")
|
|
|
|
} else {
|
|
|
|
anyhow!("{err_msg}")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.with_context(ctx)?;
|
|
|
|
|
2023-03-11 03:26:24 +00:00
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
|
2023-03-03 01:38:53 +00:00
|
|
|
fn load_roles(&mut self) -> Result<()> {
|
2023-03-03 04:43:34 +00:00
|
|
|
let path = Self::roles_file()?;
|
2023-03-03 01:38:53 +00:00
|
|
|
if !path.exists() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
let content = read_to_string(&path)
|
2023-03-05 08:45:42 +00:00
|
|
|
.with_context(|| format!("Failed to load roles at {}", path.display()))?;
|
2023-03-03 01:38:53 +00:00
|
|
|
let roles: Vec<Role> =
|
2023-03-05 08:45:42 +00:00
|
|
|
serde_yaml::from_str(&content).with_context(|| "Invalid roles config")?;
|
2023-03-03 01:38:53 +00:00
|
|
|
self.roles = roles;
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-03-11 03:26:24 +00:00
|
|
|
|
2023-11-01 02:28:54 +00:00
|
|
|
fn setup_model_info(&mut self) -> Result<()> {
|
2023-10-31 08:50:08 +00:00
|
|
|
let model = match &self.model {
|
|
|
|
Some(v) => v.clone(),
|
|
|
|
None => {
|
2023-11-02 07:33:22 +00:00
|
|
|
let models = list_models(self);
|
2023-10-31 08:50:08 +00:00
|
|
|
if models.is_empty() {
|
|
|
|
bail!("No available model");
|
|
|
|
}
|
|
|
|
|
2023-11-02 07:33:22 +00:00
|
|
|
models[0].id()
|
2023-10-31 08:50:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
self.set_model(&model)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-11-01 02:28:54 +00:00
|
|
|
fn setup_highlight(&mut self) {
|
2023-03-11 13:45:34 +00:00
|
|
|
if let Ok(value) = env::var("NO_COLOR") {
|
|
|
|
let mut no_color = false;
|
|
|
|
set_bool(&mut no_color, &value);
|
|
|
|
if no_color {
|
|
|
|
self.highlight = false;
|
|
|
|
}
|
|
|
|
}
|
2023-03-11 03:26:24 +00:00
|
|
|
}
|
2023-03-12 00:31:06 +00:00
|
|
|
|
2023-11-01 02:28:54 +00:00
|
|
|
fn setup_light_theme(&mut self) -> Result<()> {
|
2023-10-29 09:22:23 +00:00
|
|
|
if self.light_theme {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
if let Ok(value) = env::var(get_env_name("light_theme")) {
|
|
|
|
set_bool(&mut self.light_theme, &value);
|
|
|
|
return Ok(());
|
2023-10-30 03:45:07 +00:00
|
|
|
} else if let Ok(value) = env::var("COLORFGBG") {
|
|
|
|
if let Some(light) = light_theme_from_colorfgbg(&value) {
|
|
|
|
self.light_theme = light
|
|
|
|
}
|
|
|
|
};
|
2023-10-29 09:22:23 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-26 08:42:54 +00:00
|
|
|
fn compat_old_config(&mut self, config_path: &PathBuf) -> Result<()> {
|
|
|
|
let content = read_to_string(config_path)?;
|
|
|
|
let value: serde_json::Value = serde_yaml::from_str(&content)?;
|
2023-11-01 02:28:54 +00:00
|
|
|
if value.get(CLIENTS_FIELD).is_some() {
|
2023-10-26 08:42:54 +00:00
|
|
|
return Ok(());
|
2023-03-12 00:31:06 +00:00
|
|
|
}
|
2023-10-26 08:42:54 +00:00
|
|
|
|
|
|
|
if let Some(model_name) = value.get("model").and_then(|v| v.as_str()) {
|
|
|
|
if model_name.starts_with("gpt") {
|
2023-10-31 08:50:08 +00:00
|
|
|
self.model = Some(format!("{}:{}", OpenAIClient::NAME, model_name));
|
2023-10-26 08:42:54 +00:00
|
|
|
}
|
2023-03-12 00:31:06 +00:00
|
|
|
}
|
2023-10-26 08:42:54 +00:00
|
|
|
|
|
|
|
if let Some(ClientConfig::OpenAI(client_config)) = self.clients.get_mut(0) {
|
|
|
|
if let Some(api_key) = value.get("api_key").and_then(|v| v.as_str()) {
|
|
|
|
client_config.api_key = Some(api_key.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(organization_id) = value.get("organization_id").and_then(|v| v.as_str()) {
|
|
|
|
client_config.organization_id = Some(organization_id.to_string())
|
|
|
|
}
|
|
|
|
|
2023-10-31 08:50:08 +00:00
|
|
|
let mut extra_config = ExtraConfig::default();
|
|
|
|
|
2023-10-26 08:42:54 +00:00
|
|
|
if let Some(proxy) = value.get("proxy").and_then(|v| v.as_str()) {
|
2023-10-31 08:50:08 +00:00
|
|
|
extra_config.proxy = Some(proxy.to_string())
|
2023-10-26 08:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(connect_timeout) = value.get("connect_timeout").and_then(|v| v.as_i64()) {
|
2023-10-31 08:50:08 +00:00
|
|
|
extra_config.connect_timeout = Some(connect_timeout as _)
|
2023-10-26 08:42:54 +00:00
|
|
|
}
|
2023-10-31 08:50:08 +00:00
|
|
|
|
|
|
|
client_config.extra = Some(extra_config);
|
2023-10-26 08:42:54 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
2023-03-12 00:31:06 +00:00
|
|
|
}
|
2023-03-02 11:52:11 +00:00
|
|
|
}
|
|
|
|
|
2023-10-27 13:22:24 +00:00
|
|
|
#[derive(Debug, Clone, Deserialize, Default)]
|
|
|
|
pub enum Keybindings {
|
|
|
|
#[serde(rename = "emacs")]
|
|
|
|
#[default]
|
|
|
|
Emacs,
|
|
|
|
#[serde(rename = "vi")]
|
|
|
|
Vi,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Keybindings {
|
|
|
|
pub fn is_vi(&self) -> bool {
|
|
|
|
matches!(self, Keybindings::Vi)
|
|
|
|
}
|
|
|
|
pub fn stringify(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
Keybindings::Emacs => "emacs",
|
|
|
|
Keybindings::Vi => "vi",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 09:53:29 +00:00
|
|
|
fn create_config_file(config_path: &Path) -> Result<()> {
|
|
|
|
let ans = Confirm::new("No config file, create a new one?")
|
|
|
|
.with_default(true)
|
|
|
|
.prompt()
|
2023-10-31 08:50:08 +00:00
|
|
|
.map_err(prompt_op_err)?;
|
2023-03-03 09:53:29 +00:00
|
|
|
if !ans {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2023-11-01 02:28:54 +00:00
|
|
|
let client = Select::new("Platform:", list_client_types())
|
2023-03-03 09:53:29 +00:00
|
|
|
.prompt()
|
2023-10-31 08:50:08 +00:00
|
|
|
.map_err(prompt_op_err)?;
|
2023-10-26 08:42:54 +00:00
|
|
|
|
2023-11-01 02:28:54 +00:00
|
|
|
let mut config = serde_json::json!({});
|
|
|
|
config["model"] = client.into();
|
|
|
|
config[CLIENTS_FIELD] = create_client_config(client)?;
|
2023-10-26 08:42:54 +00:00
|
|
|
|
2023-11-01 02:28:54 +00:00
|
|
|
let config_data = serde_yaml::to_string(&config).with_context(|| "Failed to create config")?;
|
2023-03-03 09:53:29 +00:00
|
|
|
|
2023-03-11 03:26:24 +00:00
|
|
|
ensure_parent_exists(config_path)?;
|
2023-11-01 02:28:54 +00:00
|
|
|
std::fs::write(config_path, config_data).with_context(|| "Failed to write to config file")?;
|
2023-05-04 08:29:34 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::prelude::PermissionsExt;
|
|
|
|
let perms = std::fs::Permissions::from_mode(0o600);
|
|
|
|
std::fs::set_permissions(config_path, perms)?;
|
|
|
|
}
|
2023-10-31 09:34:16 +00:00
|
|
|
|
|
|
|
println!("✨ Saved config file to {}\n", config_path.display());
|
|
|
|
|
2023-03-03 09:53:29 +00:00
|
|
|
Ok(())
|
2023-03-02 11:52:11 +00:00
|
|
|
}
|
2023-03-05 11:48:22 +00:00
|
|
|
|
2023-03-11 03:26:24 +00:00
|
|
|
fn ensure_parent_exists(path: &Path) -> Result<()> {
|
|
|
|
if path.exists() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
let parent = path
|
|
|
|
.parent()
|
|
|
|
.ok_or_else(|| anyhow!("Failed to write to {}, No parent path", path.display()))?;
|
|
|
|
if !parent.exists() {
|
|
|
|
create_dir_all(parent).with_context(|| {
|
|
|
|
format!(
|
|
|
|
"Failed to write {}, Cannot create parent directory",
|
|
|
|
path.display()
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-11 10:53:36 +00:00
|
|
|
fn set_bool(target: &mut bool, value: &str) {
|
|
|
|
match value {
|
|
|
|
"1" | "true" => *target = true,
|
|
|
|
"0" | "false" => *target = false,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|